fix: LwHttp cache writes
This commit is contained in:
parent
dc74949736
commit
154da99d40
@ -57,7 +57,6 @@ public class LwHttp {
|
|||||||
private Socket mSocket;
|
private Socket mSocket;
|
||||||
private OutputStream mCommandStream;
|
private OutputStream mCommandStream;
|
||||||
private Buffer mResponseStream;
|
private Buffer mResponseStream;
|
||||||
private OutputStream mCacheOutputStream;
|
|
||||||
private long mLastRequest = 0;
|
private long mLastRequest = 0;
|
||||||
private SocketAddress mSockAddr;
|
private SocketAddress mSockAddr;
|
||||||
|
|
||||||
@ -109,7 +108,7 @@ 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 mCacheOutputstream;
|
OutputStream mCache;
|
||||||
int sumRead = 0;
|
int sumRead = 0;
|
||||||
int mContentLength;
|
int mContentLength;
|
||||||
|
|
||||||
@ -118,7 +117,7 @@ public class LwHttp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setCache(OutputStream cache) {
|
public void setCache(OutputStream cache) {
|
||||||
mCacheOutputstream = cache;
|
mCache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(int length) {
|
public void start(int length) {
|
||||||
@ -126,6 +125,10 @@ public class LwHttp {
|
|||||||
mContentLength = length;
|
mContentLength = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean finishedReading() {
|
||||||
|
return sumRead == mContentLength;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
if (sumRead >= mContentLength)
|
if (sumRead >= mContentLength)
|
||||||
@ -138,8 +141,8 @@ public class LwHttp {
|
|||||||
if (DBG)
|
if (DBG)
|
||||||
log.debug("read {} {}", sumRead, mContentLength);
|
log.debug("read {} {}", sumRead, mContentLength);
|
||||||
|
|
||||||
if (mCacheOutputstream != null)
|
if (mCache != null)
|
||||||
mCacheOutputstream.write(data);
|
mCache.write(data);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@ -152,20 +155,20 @@ public class LwHttp {
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int len = super.read(buffer, offset, byteCount);
|
int len = super.read(buffer, offset, byteCount);
|
||||||
sumRead += len;
|
|
||||||
|
|
||||||
if (DBG)
|
if (DBG)
|
||||||
log.debug("read {} {} {}", len, sumRead, mContentLength);
|
log.debug("read {} {} {}", len, sumRead, mContentLength);
|
||||||
|
|
||||||
if (mCacheOutputstream != null)
|
if (len <= 0)
|
||||||
mCacheOutputstream.write(buffer, offset, byteCount);
|
return len;
|
||||||
|
|
||||||
|
sumRead += len;
|
||||||
|
|
||||||
|
if (mCache != null)
|
||||||
|
mCache.write(buffer, offset, len);
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getArray() {
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
@ -256,8 +259,6 @@ public class LwHttp {
|
|||||||
is.reset();
|
is.reset();
|
||||||
is.mark(0);
|
is.mark(0);
|
||||||
is.skip(end);
|
is.skip(end);
|
||||||
|
|
||||||
is.setCache(mCacheOutputStream);
|
|
||||||
is.start(contentLength);
|
is.start(contentLength);
|
||||||
|
|
||||||
if (mInflateContent)
|
if (mInflateContent)
|
||||||
@ -376,16 +377,26 @@ public class LwHttp {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOutputStream(OutputStream outputStream) {
|
public void setCache(OutputStream os) {
|
||||||
mCacheOutputStream = outputStream;
|
mResponseStream.setCache(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestCompleted(boolean keepConnection) {
|
public boolean requestCompleted(boolean success) {
|
||||||
mLastRequest = System.nanoTime();
|
mLastRequest = System.nanoTime();
|
||||||
mCacheOutputStream = null;
|
mResponseStream.setCache(null);
|
||||||
|
|
||||||
if (!keepConnection)
|
if (!mResponseStream.finishedReading()) {
|
||||||
|
log.debug("invalid buffer position");
|
||||||
close();
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,6 +24,8 @@ import java.net.UnknownHostException;
|
|||||||
|
|
||||||
import org.oscim.tiling.MapTile;
|
import org.oscim.tiling.MapTile;
|
||||||
import org.oscim.tiling.source.ITileCache;
|
import org.oscim.tiling.source.ITileCache;
|
||||||
|
import org.oscim.tiling.source.ITileCache.TileReader;
|
||||||
|
import org.oscim.tiling.source.ITileCache.TileWriter;
|
||||||
import org.oscim.tiling.source.ITileDataSink;
|
import org.oscim.tiling.source.ITileDataSink;
|
||||||
import org.oscim.tiling.source.ITileDataSource;
|
import org.oscim.tiling.source.ITileDataSource;
|
||||||
import org.oscim.tiling.source.ITileDecoder;
|
import org.oscim.tiling.source.ITileDecoder;
|
||||||
@ -38,30 +40,27 @@ public class UrlTileDataSource implements ITileDataSource {
|
|||||||
protected final LwHttp mConn;
|
protected final LwHttp mConn;
|
||||||
protected final ITileDecoder mTileDecoder;
|
protected final ITileDecoder mTileDecoder;
|
||||||
protected final ITileCache mTileCache;
|
protected final ITileCache mTileCache;
|
||||||
|
protected final boolean mUseCache;
|
||||||
|
|
||||||
public UrlTileDataSource(TileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) {
|
public UrlTileDataSource(TileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) {
|
||||||
mTileDecoder = tileDecoder;
|
mTileDecoder = tileDecoder;
|
||||||
mTileCache = tileSource.tileCache;
|
mTileCache = tileSource.tileCache;
|
||||||
|
mUseCache = (mTileCache != null);
|
||||||
mConn = conn;
|
mConn = conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryResult executeQuery(MapTile tile, ITileDataSink sink) {
|
public QueryResult executeQuery(MapTile tile, ITileDataSink sink) {
|
||||||
|
if (mUseCache) {
|
||||||
ITileCache.TileWriter cacheWriter = null;
|
TileReader c = mTileCache.getTile(tile);
|
||||||
|
if (c != null) {
|
||||||
if (mTileCache != null) {
|
|
||||||
ITileCache.TileReader c = mTileCache.getTile(tile);
|
|
||||||
if (c == null) {
|
|
||||||
cacheWriter = mTileCache.writeTile(tile);
|
|
||||||
} else {
|
|
||||||
InputStream is = c.getInputStream();
|
InputStream is = c.getInputStream();
|
||||||
try {
|
try {
|
||||||
if (mTileDecoder.decode(tile, sink, is)) {
|
if (mTileDecoder.decode(tile, sink, is))
|
||||||
return QueryResult.SUCCESS;
|
return QueryResult.SUCCESS;
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.debug("{} Cache read: {}", tile, e);
|
||||||
} finally {
|
} finally {
|
||||||
IOUtils.closeQuietly(is);
|
IOUtils.closeQuietly(is);
|
||||||
}
|
}
|
||||||
@ -69,16 +68,19 @@ public class UrlTileDataSource implements ITileDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
|
TileWriter cache = null;
|
||||||
try {
|
try {
|
||||||
if (cacheWriter != null)
|
|
||||||
mConn.setOutputStream(cacheWriter.getOutputStream());
|
|
||||||
|
|
||||||
InputStream is;
|
InputStream is;
|
||||||
if (!mConn.sendRequest(tile)) {
|
if (!mConn.sendRequest(tile)) {
|
||||||
log.debug("{} Request failed", tile);
|
log.debug("{} Request failed", tile);
|
||||||
} else if ((is = mConn.readHeader()) == null) {
|
} else if ((is = mConn.readHeader()) == null) {
|
||||||
log.debug("{} Network Error", tile);
|
log.debug("{} Network Error", tile);
|
||||||
} else {
|
} else {
|
||||||
|
if (mUseCache) {
|
||||||
|
cache = mTileCache.writeTile(tile);
|
||||||
|
mConn.setCache(cache.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
success = mTileDecoder.decode(tile, sink, is);
|
success = mTileDecoder.decode(tile, sink, is);
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
@ -92,8 +94,8 @@ public class UrlTileDataSource implements ITileDataSource {
|
|||||||
} finally {
|
} finally {
|
||||||
mConn.requestCompleted(success);
|
mConn.requestCompleted(success);
|
||||||
|
|
||||||
if (cacheWriter != null)
|
if (cache != null)
|
||||||
cacheWriter.complete(success);
|
cache.complete(success);
|
||||||
}
|
}
|
||||||
return success ? QueryResult.SUCCESS : QueryResult.FAILED;
|
return success ? QueryResult.SUCCESS : QueryResult.FAILED;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user