LwHttp: take skip() position into account for writing to cache
This commit is contained in:
parent
305032707b
commit
1bda9aff3f
@ -109,38 +109,40 @@ public class LwHttp {
|
|||||||
// to avoid a copy in PbfDecoder one could manage the buffer
|
// to avoid a copy in PbfDecoder one could manage the buffer
|
||||||
// array directly and provide access to it.
|
// array directly and provide access to it.
|
||||||
static class Buffer extends BufferedInputStream {
|
static class Buffer extends BufferedInputStream {
|
||||||
OutputStream mCache;
|
OutputStream cache;
|
||||||
int sumRead = 0;
|
int bytesRead = 0;
|
||||||
|
int bytesWrote;
|
||||||
int marked = -1;
|
int marked = -1;
|
||||||
int mContentLength;
|
int contentLength;
|
||||||
|
|
||||||
public Buffer(InputStream is) {
|
public Buffer(InputStream is) {
|
||||||
super(is, BUFFER_SIZE);
|
super(is, BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCache(OutputStream cache) {
|
public void setCache(OutputStream cache) {
|
||||||
mCache = cache;
|
this.cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(int length) {
|
public void start(int length) {
|
||||||
sumRead = 0;
|
bytesRead = 0;
|
||||||
mContentLength = length;
|
bytesWrote = 0;
|
||||||
|
contentLength = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean finishedReading() {
|
public boolean finishedReading() {
|
||||||
return sumRead == mContentLength;
|
return bytesRead == contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void mark(int readlimit) {
|
public synchronized void mark(int readlimit) {
|
||||||
marked = sumRead;
|
marked = bytesRead;
|
||||||
super.mark(readlimit);
|
super.mark(readlimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized long skip(long n) throws IOException {
|
public synchronized long skip(long n) throws IOException {
|
||||||
// Android Image decoder *requires* skip to
|
// Android(4.1.2) image decoder *requires* skip to
|
||||||
// actually skip the requested amout.
|
// actually skip the requested amount.
|
||||||
// https://code.google.com/p/android/issues/detail?id=6066
|
// https://code.google.com/p/android/issues/detail?id=6066
|
||||||
long sumSkipped = 0L;
|
long sumSkipped = 0L;
|
||||||
while (sumSkipped < n) {
|
while (sumSkipped < n) {
|
||||||
@ -153,33 +155,37 @@ public class LwHttp {
|
|||||||
break; // EOF
|
break; // EOF
|
||||||
|
|
||||||
sumSkipped += 1;
|
sumSkipped += 1;
|
||||||
|
// was incremented by read()
|
||||||
|
bytesRead -= 1;
|
||||||
}
|
}
|
||||||
sumRead += sumSkipped;
|
bytesRead += sumSkipped;
|
||||||
return sumSkipped;
|
return sumSkipped;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void reset() throws IOException {
|
public synchronized void reset() throws IOException {
|
||||||
if (marked >= 0)
|
if (marked >= 0)
|
||||||
sumRead = marked;
|
bytesRead = marked;
|
||||||
// TODO could check if the mark is already invalid
|
// TODO could check if the mark is already invalid
|
||||||
super.reset();
|
super.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
if (sumRead >= mContentLength)
|
if (bytesRead >= contentLength)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int data = super.read();
|
int data = super.read();
|
||||||
|
|
||||||
sumRead += 1;
|
bytesRead += 1;
|
||||||
|
|
||||||
if (dbg)
|
if (dbg)
|
||||||
log.debug("read {} {}", sumRead, mContentLength);
|
log.debug("read {} {}", bytesRead, contentLength);
|
||||||
|
|
||||||
if (mCache != null)
|
if (cache != null && bytesRead > bytesWrote) {
|
||||||
mCache.write(data);
|
bytesWrote = bytesRead;
|
||||||
|
cache.write(data);
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@ -188,21 +194,24 @@ public class LwHttp {
|
|||||||
public int read(byte[] buffer, int offset, int byteCount)
|
public int read(byte[] buffer, int offset, int byteCount)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
if (sumRead >= mContentLength)
|
if (bytesRead >= contentLength)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int len = super.read(buffer, offset, byteCount);
|
int len = super.read(buffer, offset, byteCount);
|
||||||
|
|
||||||
if (dbg)
|
if (dbg)
|
||||||
log.debug("read {} {} {}", len, sumRead, mContentLength);
|
log.debug("read {} {} {}", len, bytesRead, contentLength);
|
||||||
|
|
||||||
if (len <= 0)
|
if (len <= 0)
|
||||||
return len;
|
return len;
|
||||||
|
|
||||||
sumRead += len;
|
bytesRead += len;
|
||||||
|
|
||||||
if (mCache != null)
|
if (cache != null && bytesRead > bytesWrote) {
|
||||||
mCache.write(buffer, offset, len);
|
int add = bytesRead - bytesWrote;
|
||||||
|
bytesWrote = bytesRead;
|
||||||
|
cache.write(buffer, offset + (len - add), add);
|
||||||
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user