From 3543b7167118b9557c6e589e3ed5a7897dba2f93 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Tue, 28 Jan 2014 00:31:52 +0100 Subject: [PATCH] fix NPE in LwHttp hopefully this fixes #27, check mResposeStream != null. release references to streams when socket is closed() --- .../oscim/tiling/source/common/LwHttp.java | 124 +++++++++--------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/vtm/src/org/oscim/tiling/source/common/LwHttp.java b/vtm/src/org/oscim/tiling/source/common/LwHttp.java index c57a2e4c..d7596a2f 100644 --- a/vtm/src/org/oscim/tiling/source/common/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/common/LwHttp.java @@ -27,6 +27,7 @@ import java.net.URL; import org.oscim.core.Tile; import org.oscim.utils.ArrayUtils; +import org.oscim.utils.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -212,18 +213,6 @@ public class LwHttp { } } - public void close() { - if (mSocket != null) { - try { - mSocket.close(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - mSocket = null; - } - } - } - public InputStream read() throws IOException { Buffer is = mResponseStream; @@ -306,30 +295,21 @@ public class LwHttp { public boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException { - if (mSocket != null && ((mMaxReq-- <= 0) - || (System.nanoTime() - mLastRequest > RESPONSE_TIMEOUT))) { - - close(); - - if (dbg) - log.debug("not alive - recreate connection " + mMaxReq); + if (mSocket != null) { + if (mMaxReq-- <= 0) + close(); + else if (System.nanoTime() - mLastRequest > RESPONSE_TIMEOUT) + close(); + else if (mResponseStream.available() > 0) + close(); } if (mSocket == null) { + // might throw IOException lwHttpConnect(); - // we know our server + + // TODO parse from header mMaxReq = RESPONSE_EXPECTED_LIVES; - // log.debug("create connection"); - } else { - int avail = mResponseStream.available(); - if (avail > 0) { - log.debug("left-over bytes: " + avail); - close(); - lwHttpConnect(); - // FIXME not sure if this is correct way to drain socket - //while ((avail = mResponseStream.available()) > 0) - // mResponseStream.read(buffer); - } } byte[] request = mRequestBuffer; @@ -350,13 +330,14 @@ public class LwHttp { return true; } catch (IOException e) { log.debug("recreate connection"); + close(); + // might throw IOException + lwHttpConnect(); + + mCommandStream.write(request, 0, len); + mCommandStream.flush(); } - lwHttpConnect(); - - mCommandStream.write(request, 0, len); - mCommandStream.flush(); - return true; } @@ -364,13 +345,52 @@ public class LwHttp { if (mSockAddr == null) mSockAddr = new InetSocketAddress(mHost, mPort); - mSocket = new Socket(); - mSocket.connect(mSockAddr, 30000); - mSocket.setTcpNoDelay(true); + try { + mSocket = new Socket(); + mSocket.connect(mSockAddr, 30000); + mSocket.setTcpNoDelay(true); + mCommandStream = mSocket.getOutputStream(); + mResponseStream = new Buffer(mSocket.getInputStream()); + } catch (IOException e) { + close(); + throw e; + } + return true; + } - mCommandStream = mSocket.getOutputStream(); - mResponseStream = new Buffer(mSocket.getInputStream()); + public void close() { + if (mSocket == null) + return; + IOUtils.closeQuietly(mSocket); + mSocket = null; + mCommandStream = null; + mResponseStream = null; + } + + public void setCache(OutputStream os) { + if (mResponseStream == null) + return; + + mResponseStream.setCache(os); + } + + public boolean requestCompleted(boolean success) { + if (mResponseStream == null) + return false; + + mLastRequest = System.nanoTime(); + mResponseStream.setCache(null); + + if (!mResponseStream.finishedReading()) { + log.debug("invalid buffer position"); + close(); + return false; + } + if (!success) { + close(); + return false; + } return true; } @@ -413,26 +433,4 @@ public class LwHttp { return true; } - - public void setCache(OutputStream os) { - mResponseStream.setCache(os); - } - - public boolean requestCompleted(boolean success) { - mLastRequest = System.nanoTime(); - mResponseStream.setCache(null); - - if (!mResponseStream.finishedReading()) { - log.debug("invalid buffer position"); - close(); - return false; - } - - if (!success) { - close(); - return false; - } - - return true; - } }