Don't read from the delegate stream after we close it.

Change-Id: I9a018ca88373d5f317335e35fc6ca43c5473490e
http://b/4188137
diff --git a/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/UnknownLengthHttpInputStream.java b/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/UnknownLengthHttpInputStream.java
index 7bb4abb..574f509 100644
--- a/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/UnknownLengthHttpInputStream.java
+++ b/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/UnknownLengthHttpInputStream.java
@@ -34,7 +34,7 @@
     @Override public int read(byte[] buffer, int offset, int count) throws IOException {
         checkBounds(buffer, offset, count);
         checkNotClosed();
-        if (in == null) {
+        if (in == null || inputExhausted) {
             return -1;
         }
         int read = in.read(buffer, offset, count);
diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
index e3394a5..5bffcfe 100644
--- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
@@ -1501,6 +1501,24 @@
     }
 
     /**
+     * http://code.google.com/p/android/issues/detail?id=14562
+     */
+    public void testReadAfterLastByte() throws Exception {
+        server.enqueue(new MockResponse()
+                .setBody("ABC")
+                .clearHeaders()
+                .addHeader("Connection: close")
+                .setDisconnectAtEnd(true));
+        server.play();
+
+        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
+        InputStream in = connection.getInputStream();
+        assertEquals("ABC", readAscii(in, 3));
+        assertEquals(-1, in.read());
+        assertEquals(-1, in.read()); // throws IOException in Gingerbread
+    }
+
+    /**
      * Encodes the response body using GZIP and adds the corresponding header.
      */
     public byte[] gzip(byte[] bytes) throws IOException {
diff --git a/support/src/test/java/tests/http/MockWebServer.java b/support/src/test/java/tests/http/MockWebServer.java
index d790399..25d555c 100644
--- a/support/src/test/java/tests/http/MockWebServer.java
+++ b/support/src/test/java/tests/http/MockWebServer.java
@@ -32,12 +32,12 @@
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingDeque;
@@ -59,7 +59,7 @@
     private final BlockingQueue<MockResponse> responseQueue
             = new LinkedBlockingDeque<MockResponse>();
     private final Set<Socket> openClientSockets
-            = Collections.synchronizedSet(new HashSet<Socket>());
+            = Collections.newSetFromMap(new ConcurrentHashMap<Socket, Boolean>());
     private boolean singleResponse;
     private final AtomicInteger requestCount = new AtomicInteger();
     private int bodyLimit = Integer.MAX_VALUE;