summaryrefslogtreecommitdiffstats
path: root/luni/src
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2011-09-30 11:31:04 -0700
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-09-30 11:31:04 -0700
commitbdd37a76ddfd92d6716127e025ba9cf733e66fc4 (patch)
treecc5d85e90f13cb577490f0c57897db8e6bd86bb0 /luni/src
parent8f0e611282bccb0a96b716c40cda4d1ba31545b2 (diff)
parent25a753691a80186cd4d7086b12c0e52225d95897 (diff)
downloadlibcore-bdd37a76ddfd92d6716127e025ba9cf733e66fc4.zip
libcore-bdd37a76ddfd92d6716127e025ba9cf733e66fc4.tar.gz
libcore-bdd37a76ddfd92d6716127e025ba9cf733e66fc4.tar.bz2
Merge "Fix chunked input stream's available() method to never return -1."
Diffstat (limited to 'luni/src')
-rw-r--r--luni/src/main/java/libcore/net/http/ChunkedInputStream.java5
-rw-r--r--luni/src/test/java/libcore/java/net/URLConnectionTest.java29
2 files changed, 33 insertions, 1 deletions
diff --git a/luni/src/main/java/libcore/net/http/ChunkedInputStream.java b/luni/src/main/java/libcore/net/http/ChunkedInputStream.java
index 380ed02..91cff7a 100644
--- a/luni/src/main/java/libcore/net/http/ChunkedInputStream.java
+++ b/luni/src/main/java/libcore/net/http/ChunkedInputStream.java
@@ -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 {
diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
index 345f324..acd3c13 100644
--- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
@@ -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}.
*/