Skip to content

Commit

Permalink
fix(tcp-window): fix download useless data on tcp-window because of t…
Browse files Browse the repository at this point in the history
…he first connection use 0-infinite range

closes #933
  • Loading branch information
Jacksgong committed Jan 31, 2018
1 parent 6567402 commit e175b7b
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@
/**
* The connection profile for {@link ConnectTask}.
*/

public class ConnectionProfile {

static final int RANGE_INFINITE = -1;

final long startOffset;
final long currentOffset;
final long endOffset;
final long contentLength;

private final boolean isForceNoRange;

ConnectionProfile(long startOffset, long currentOffset, long endOffset, long contentLength) {
private ConnectionProfile(long startOffset, long currentOffset, long endOffset,
long contentLength) {
this(startOffset, currentOffset, endOffset, contentLength, false);
}

ConnectionProfile(long startOffset, long currentOffset, long endOffset, long contentLength,
boolean isForceNoRange) {
private ConnectionProfile(long startOffset, long currentOffset, long endOffset,
long contentLength,
boolean isForceNoRange) {
if ((startOffset != 0 || endOffset != 0) && isForceNoRange) {
throw new IllegalArgumentException();
}
Expand All @@ -53,7 +56,7 @@ public void addRangeHeader(FileDownloadConnection connection) {
if (isForceNoRange) return;

final String range;
if (endOffset == 0) {
if (endOffset == RANGE_INFINITE) {
range = FileDownloadUtils.formatString("bytes=%d-", currentOffset);
} else {
range = FileDownloadUtils
Expand All @@ -67,4 +70,31 @@ public String toString() {
return FileDownloadUtils.formatString("range[%d, %d) current offset[%d]",
startOffset, endOffset, currentOffset);
}

public static class ConnectionProfileBuild {
public static ConnectionProfile buildTrialConnectionProfile() {
return new ConnectionProfile(0, 0, 0, 0);
}

public static ConnectionProfile buildTrialConnectionProfileNoRange() {
return new ConnectionProfile(0, 0, 0, 0, true);
}

public static ConnectionProfile buildBeginToEndConnectionProfile(long contentLength) {
return new ConnectionProfile(0, 0, RANGE_INFINITE, contentLength);
}

public static ConnectionProfile buildToEndConnectionProfile(long startOffset,
long currentOffset,
long contentLength) {
return new ConnectionProfile(startOffset, currentOffset, RANGE_INFINITE, contentLength);
}

public static ConnectionProfile buildConnectionProfile(long startOffset,
long currentOffset,
long endOffset,
long contentLength) {
return new ConnectionProfile(startOffset, currentOffset, endOffset, contentLength);
}
}
}
Loading

0 comments on commit e175b7b

Please sign in to comment.