Skip to content

Commit

Permalink
Merge "Fix chunked input stream's available() method to never return …
Browse files Browse the repository at this point in the history
…-1."
  • Loading branch information
Jesse Wilson authored and Android (Google) Code Review committed Sep 30, 2011
2 parents 8f0e611 + 25a7536 commit bdd37a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion luni/src/main/java/libcore/net/http/ChunkedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ private void readChunkSize() throws IOException {

@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 {
Expand Down
29 changes: 29 additions & 0 deletions luni/src/test/java/libcore/java/net/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,35 @@ public void testUrlContainsQueryButNoPath() throws Exception {
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}.
*/
Expand Down

0 comments on commit bdd37a7

Please sign in to comment.