Commit 25a75369 authored by Jesse Wilson's avatar Jesse Wilson
Browse files

Fix chunked input stream's available() method to never return -1.

Change-Id: I6ea268e469675ef890010282dbe35ed5838cca73
http://code.google.com/p/android/issues/detail?id=20442
parent 92cb29d7
......@@ -95,7 +95,10 @@ final class ChunkedInputStream extends AbstractHttpInputStream {
@Override public int available() throws IOException {
checkNotClosed();
return hasMoreChunks ? Math.min(in.available(), bytesRemainingInChunk) : 0;
if (!hasMoreChunks || bytesRemainingInChunk == NO_CHUNK_YET) {
return 0;
}
return Math.min(in.available(), bytesRemainingInChunk);
}
@Override public void close() throws IOException {
......
......@@ -1866,6 +1866,35 @@ public final class URLConnectionTest extends TestCase {
assertEquals("GET /?query HTTP/1.1", request.getRequestLine());
}
// http://code.google.com/p/android/issues/detail?id=20442
public void testInputStreamAvailableWithChunkedEncoding() throws Exception {
testInputStreamAvailable(TransferKind.CHUNKED);
}
public void testInputStreamAvailableWithContentLengthHeader() throws Exception {
testInputStreamAvailable(TransferKind.FIXED_LENGTH);
}
public void testInputStreamAvailableWithNoLengthHeaders() throws Exception {
testInputStreamAvailable(TransferKind.END_OF_STREAM);
}
private void testInputStreamAvailable(TransferKind transferKind) throws IOException {
String body = "ABCDEFGH";
MockResponse response = new MockResponse();
transferKind.setBody(response, body, 4);
server.enqueue(response);
server.play();
URLConnection connection = server.getUrl("/").openConnection();
InputStream in = connection.getInputStream();
for (int i = 0; i < body.length(); i++) {
assertTrue(in.available() >= 0);
assertEquals(body.charAt(i), in.read());
}
assertEquals(0, in.available());
assertEquals(-1, in.read());
}
/**
* Returns a gzipped copy of {@code bytes}.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment