8212114: Reconsider the affect on closed streams resulting from 8189366
Reviewed-by: chegar, dfuchs
--- a/src/java.base/share/classes/java/net/SocketInputStream.java Mon Oct 22 10:47:28 2018 +0100
+++ b/src/java.base/share/classes/java/net/SocketInputStream.java Mon Oct 22 15:20:43 2018 +0530
@@ -232,11 +232,8 @@
* @return the number of immediately available bytes
*/
public int available() throws IOException {
- if (eof) {
- return 0;
- } else {
- return impl.available();
- }
+ int available = impl.available();
+ return eof ? 0 : available;
}
/**
--- a/test/jdk/java/net/Socket/CloseAvailable.java Mon Oct 22 10:47:28 2018 +0100
+++ b/test/jdk/java/net/Socket/CloseAvailable.java Mon Oct 22 15:20:43 2018 +0530
@@ -40,6 +40,8 @@
testEOF(true);
testEOF(false);
+ testIOEOnClosed(true);
+ testIOEOnClosed(false);
}
static void testClose() throws IOException {
@@ -115,4 +117,40 @@
}
System.out.println("\ncomplete");
}
+
+ // Verifies IOException thrown by `available`, on a closed input stream
+ // that may, or may not, have reached EOF prior to closure.
+ static void testIOEOnClosed(boolean readUntilEOF) throws IOException {
+ System.out.println("testIOEOnClosed, readUntilEOF: " + readUntilEOF);
+ InetAddress addr = InetAddress.getLoopbackAddress();
+ ServerSocket ss = new ServerSocket();
+ ss.bind(new InetSocketAddress(addr, 0), 0);
+ int port = ss.getLocalPort();
+
+ try (Socket s = new Socket(addr, port)) {
+ s.getOutputStream().write(0x43);
+ s.shutdownOutput();
+
+ try (Socket soc = ss.accept()) {
+ ss.close();
+
+ InputStream is = soc.getInputStream();
+ int b = is.read();
+ assert b == 0x43;
+ assert !s.isClosed();
+ if (readUntilEOF) {
+ b = is.read();
+ assert b == -1;
+ }
+ is.close();
+ try {
+ b = is.available();
+ throw new RuntimeException("UNEXPECTED successful read: " + b);
+ } catch (IOException expected) {
+ System.out.println("caught expected IOException:" + expected);
+ }
+ }
+ }
+ System.out.println("\ncomplete");
+ }
}