If available, check content length before downloading. Check also

content length is not over Integer.MAX_VALUE.
pull/26/head
luc 9 years ago
parent 5bbb2e1730
commit 3c4c77099d

@ -712,31 +712,62 @@ public class HTTPClient {
} }
} }
private static byte[] getByteArray(final HttpEntity entity, final int maxBytes) throws IOException { /**
final InputStream instream = entity.getContent(); * Return entity content loaded as a byte array
if (instream == null) { * @param entity HTTP entity
return null; * @param maxBytes maximum bytes to read. -1 means no maximum limit.
} * @return content bytes or null when entity content is null.
try { * @throws IOException when a read error occured or content length is over maxBytes
int i = maxBytes < 0 ? (int)entity.getContentLength() : Math.min(maxBytes, (int)entity.getContentLength()); */
if (i < 0) { public static byte[] getByteArray(final HttpEntity entity, int maxBytes) throws IOException {
i = 4096; final InputStream instream = entity.getContent();
} if (instream == null) {
final ByteArrayBuffer buffer = new ByteArrayBuffer(i); return null;
byte[] tmp = new byte[4096]; }
int l, sum = 0; try {
while((l = instream.read(tmp)) != -1) { long contentLength = entity.getContentLength();
sum += l; /*
if (maxBytes >= 0 && sum > maxBytes) throw new IOException("Download exceeded maximum value of " + maxBytes + " bytes"); * When no maxBytes is specified, the default limit is
buffer.append(tmp, 0, l); * Integer.MAX_VALUE as a byte array size can not be over
} */
return buffer.toByteArray(); if (maxBytes < 0) {
} catch (final OutOfMemoryError e) { maxBytes = Integer.MAX_VALUE;
throw new IOException(e.toString()); }
} finally { /*
instream.close(); * Content length may already be known now : check it before
} * downloading
} */
if (contentLength > maxBytes) {
throw new IOException("Content to download exceed maximum value of " + maxBytes + " bytes");
}
int initialSize = Math.min(maxBytes, (int) contentLength);
/* ContentLenght may be negative because unknown for now */
if (initialSize < 0) {
initialSize = 4096;
}
final ByteArrayBuffer buffer = new ByteArrayBuffer(initialSize);
byte[] tmp = new byte[4096];
int l = 0;
/* Sum is a long to enable check against Integer.MAX_VALUE */
long sum = 0;
while ((l = instream.read(tmp)) != -1) {
sum += l;
/*
* Check total length while downloading as content lenght might
* not be known at beginning
*/
if (sum > maxBytes) {
throw new IOException("Download exceeded maximum value of " + maxBytes + " bytes");
}
buffer.append(tmp, 0, l);
}
return buffer.toByteArray();
} catch (final OutOfMemoryError e) {
throw new IOException(e.toString());
} finally {
instream.close();
}
}
private void setHeaders(final HttpUriRequest httpUriRequest) { private void setHeaders(final HttpUriRequest httpUriRequest) {
if (this.headers != null) { if (this.headers != null) {

Loading…
Cancel
Save