Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug when handling non-cached range responses that aren't honored by the server. #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions library/src/main/java/com/danikula/videocache/HttpProxyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,27 @@ private void responseWithoutCache(OutputStream out, long offset) throws ProxyCac
HttpUrlSource newSourceNoCache = new HttpUrlSource(this.source);
try {
newSourceNoCache.open((int) offset);

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int readBytes;

/* manually advance to the offset if the server ignored the
range request and returned the entire body. */
if (offset > 0 && newSourceNoCache.getResponseCode() == 200) {
int readCount;
long remaining = offset;
while (remaining > 0) {
readCount = (int) Math.min(DEFAULT_BUFFER_SIZE, offset);
remaining -= newSourceNoCache.read(buffer, readCount);
}
}

/* return data after the offset */
while ((readBytes = newSourceNoCache.read(buffer)) != -1) {
out.write(buffer, 0, readBytes);
offset += readBytes;
}

out.flush();
} finally {
newSourceNoCache.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ public void close() throws ProxyCacheException {

@Override
public int read(byte[] buffer) throws ProxyCacheException {
return read(buffer, buffer.length);
}

public int read(byte[] buffer, int length) throws ProxyCacheException {
if (inputStream == null) {
throw new ProxyCacheException("Error reading data from " + sourceInfo.url + ": connection is absent!");
}
try {
return inputStream.read(buffer, 0, buffer.length);
return inputStream.read(buffer, 0, length);
} catch (InterruptedIOException e) {
throw new InterruptedProxyCacheException("Reading source " + sourceInfo.url + " is interrupted", e);
} catch (IOException e) {
Expand Down Expand Up @@ -200,6 +204,10 @@ public String getUrl() {
return sourceInfo.url;
}

public int getResponseCode() throws IOException {
return connection.getResponseCode();
}

@Override
public String toString() {
return "HttpUrlSource{sourceInfo='" + sourceInfo + "}";
Expand Down