HttpEngine: throw exception instead of null return in conn.read()

- convert OkHttps FileNotFound to some more useful exception
This commit is contained in:
Hannes Janetzek 2014-09-11 20:53:06 +02:00
parent 47b2a55b11
commit 691a18873d
3 changed files with 29 additions and 32 deletions

View File

@ -252,7 +252,6 @@ public class LwHttp implements HttpEngine {
byte[] buf = buffer;
boolean first = true;
boolean ok = true;
boolean gzip = false;
int read = 0;
@ -272,7 +271,7 @@ public class LwHttp implements HttpEngine {
end++;
if (end == BUFFER_SIZE) {
return null;
throw new IOException("Header too large!");
}
if (buf[end] != '\n')
@ -284,25 +283,24 @@ public class LwHttp implements HttpEngine {
break;
}
if (ok) {
if (first) {
first = false;
/* check only for OK ("HTTP/1.? ".length == 9) */
if (!check(HEADER_HTTP_OK, buf, pos + 9, end))
ok = false;
} else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) {
/* parse Content-Length */
contentLength = parseInt(buf, pos +
HEADER_CONTENT_LENGTH.length + 2, end - 1);
} else if (check(HEADER_ENCODING_GZIP, buf, pos, end)) {
gzip = true;
} else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) {
mMustCloseConnection = true;
if (first) {
first = false;
/* check only for OK ("HTTP/1.? ".length == 9) */
if (!check(HEADER_HTTP_OK, buf, pos + 9, end)) {
throw new IOException("HTTP Error: "
+ new String(buf, pos, end - pos - 1));
}
} else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) {
/* parse Content-Length */
contentLength = parseInt(buf, pos +
HEADER_CONTENT_LENGTH.length + 2, end - 1);
} else if (check(HEADER_ENCODING_GZIP, buf, pos, end)) {
gzip = true;
} else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) {
mMustCloseConnection = true;
}
if (!ok || dbg) {
if (dbg) {
String line = new String(buf, pos, end - pos - 1);
log.debug("> {} <", line);
}
@ -311,9 +309,6 @@ public class LwHttp implements HttpEngine {
end = pos;
}
if (!ok)
return null;
/* back to start of content */
is.reset();
is.mark(0);

View File

@ -17,6 +17,7 @@
*/
package org.oscim.tiling.source;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -69,7 +70,12 @@ public class OkHttpEngine implements HttpEngine {
for (Entry<String, String> opt : mTileSource.getRequestHeader().entrySet())
conn.addRequestProperty(opt.getKey(), opt.getValue());
inputStream = conn.getInputStream();
try {
inputStream = conn.getInputStream();
} catch (FileNotFoundException e) {
throw new IOException("ERROR " + conn.getResponseCode()
+ ": " + conn.getResponseMessage());
}
}
@Override

View File

@ -76,23 +76,19 @@ public class UrlTileDataSource implements ITileDataSource {
try {
mConn.sendRequest(tile);
InputStream is = mConn.read();
if (is == null) {
log.debug("{} Network Error", tile);
} else {
if (mUseCache) {
cacheWriter = cache.writeTile(tile);
mConn.setCache(cacheWriter.getOutputStream());
}
success = mTileDecoder.decode(tile, sink, is);
if (mUseCache) {
cacheWriter = cache.writeTile(tile);
mConn.setCache(cacheWriter.getOutputStream());
}
success = mTileDecoder.decode(tile, sink, is);
} catch (SocketException e) {
log.debug("{} Socket exception: {}", tile, e.getMessage());
log.debug("{} Socket Error: {}", tile, e.getMessage());
} catch (SocketTimeoutException e) {
log.debug("{} Socket Timeout", tile);
} catch (UnknownHostException e) {
log.debug("{} Unknown host: {}", tile, e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.debug("{} Network Error: {}", tile, e.getMessage());
} finally {
success = mConn.requestCompleted(success);
if (cacheWriter != null)