11/*
2- * Copyright (c) 1998, 2019 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1998, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525 * @test
2626 * @bug 4091859 8189366
2727 * @library /test/lib
28- * @summary Test Socket.available()
28+ * @summary Test Socket.getInputStream(). available()
2929 * @run main CloseAvailable
3030 * @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable
3131 */
@@ -48,110 +48,141 @@ public static void main(String[] args) throws Exception {
4848 testIOEOnClosed (false );
4949 }
5050
51+ /*
52+ * Verifies that the Socket.getInputStream().available() throws an IOException
53+ * if invoked after the socket has been closed.
54+ */
5155 static void testClose () throws IOException {
52- boolean error = true ;
53- InetAddress addr = InetAddress .getLocalHost ();
54- ServerSocket ss = new ServerSocket (0 , 0 , addr );
55- int port = ss .getLocalPort ();
56-
57- Thread t = new Thread (new Thread ("Close-Available-1" ) {
58- public void run () {
59- try {
60- Socket s = new Socket (addr , port );
61- s .close ();
62- } catch (Exception e ) {
63- e .printStackTrace ();
64- }
65- }
66- });
67-
68- t .start ();
69-
70- Socket soc = ss .accept ();
71- ss .close ();
72-
73- DataInputStream is = new DataInputStream (soc .getInputStream ());
74- is .close ();
75-
56+ System .out .println ("testClose" );
57+ final InetAddress addr = InetAddress .getLoopbackAddress ();
58+ final Socket acceptedSocket ;
59+ try (final ServerSocket ss = new ServerSocket (0 , 0 , addr )) {
60+ System .out .println ("created server socket: " + ss );
61+ final int port = ss .getLocalPort ();
62+ // start a thread which initiates a socket connection to the server
63+ //Thread.ofPlatform().name("Close-Available-1")
64+ // .start(() -> {
65+ Thread t = new Thread (new Thread ("Close-Available-1" ) {
66+ public void run () {
67+ try {
68+ final Socket s = new Socket (addr , port );
69+ System .out .println ("created socket: " + s );
70+ s .close ();
71+ System .out .println ("closed socket: " + s );
72+ } catch (Exception e ) {
73+ System .err .println ("exception in " + Thread .currentThread ().getName ()
74+ + ": " + e );
75+ e .printStackTrace ();
76+ }
77+ }
78+ });
79+
80+ t .start ();
81+
82+ // accept the client connect
83+ acceptedSocket = ss .accept ();
84+ System .out .println (ss + " accepted connection " + acceptedSocket );
85+ } // (intentionally) close the ServerSocket
86+
87+ final DataInputStream is = new DataInputStream (acceptedSocket .getInputStream ());
88+ is .close (); // close the inputstream and thus the underlying socket
89+ System .out .println ("closed inputstream of socket: " + acceptedSocket );
7690 try {
77- is .available ();
78- }
79- catch (IOException ex ) {
80- error = false ;
91+ final int av = is .available ();
92+ // available() was expected to fail but didn't
93+ throw new AssertionError ("Socket.getInputStream().available() was expected to fail on "
94+ + acceptedSocket + " but returned " + av );
95+ } catch (IOException ex ) {
96+ // expected IOException
97+ System .out .println ("received the expected IOException: " + ex );
8198 }
82- if (error )
83- throw new RuntimeException ("Available() can be called after stream closed." );
8499 }
85100
86- // Verifies consistency of `available` behaviour when EOF reached, both
87- // explicitly and implicitly.
101+ /*
102+ * Verifies consistency of Socket.getInputStream().available() behaviour when EOF reached, both
103+ * explicitly and implicitly.
104+ */
88105 static void testEOF (boolean readUntilEOF ) throws IOException {
89106 System .out .println ("testEOF, readUntilEOF: " + readUntilEOF );
90- InetAddress addr = InetAddress .getLoopbackAddress ();
91- ServerSocket ss = new ServerSocket ();
92- ss .bind (new InetSocketAddress (addr , 0 ), 0 );
93- int port = ss .getLocalPort ();
94-
95- try (Socket s = new Socket (addr , port )) {
96- s .getOutputStream ().write (0x42 );
97- s .shutdownOutput ();
98-
99- try (Socket soc = ss .accept ()) {
100- ss .close ();
101-
102- InputStream is = soc .getInputStream ();
103- int b = is .read ();
104- assert b == 0x42 ;
105- assert !s .isClosed ();
106- if (readUntilEOF ) {
107- b = is .read ();
108- assert b == -1 ;
107+ final InetAddress addr = InetAddress .getLoopbackAddress ();
108+ try (final ServerSocket ss = new ServerSocket ()) {
109+ ss .bind (new InetSocketAddress (addr , 0 ), 0 );
110+ System .out .println ("server socket bound: " + ss );
111+ final int port = ss .getLocalPort ();
112+ try (final Socket s = new Socket (addr , port )) {
113+ System .out .println ("created socket: " + s );
114+ s .getOutputStream ().write (0x42 );
115+ s .shutdownOutput ();
116+
117+ try (final Socket soc = ss .accept ()) {
118+ System .out .println ("accepted socket: " + soc );
119+ ss .close ();
120+ System .out .println ("closed server socket: " + ss );
121+
122+ final InputStream is = soc .getInputStream ();
123+ int b = is .read ();
124+ assert b == 0x42 : "unexpected byte read: " + b ;
125+ assert !s .isClosed () : "socket " + s + " is unexpectedly closed" ;
126+ if (readUntilEOF ) {
127+ b = is .read ();
128+ assert b == -1 : "unexpected number of bytes read: " + b ;
129+ }
130+
131+ int a ;
132+ for (int i = 0 ; i < 100 ; i ++) {
133+ a = is .available ();
134+ System .out .print (a + ", " );
135+ if (a != 0 ) {
136+ throw new RuntimeException ("Unexpected non-zero available: " + a );
137+ }
138+ }
139+ assert !s .isClosed () : "socket " + s + " is unexpectedly closed" ;
140+ final int more = is .read ();
141+ assert more == -1 : "unexpected byte read: " + more ;
109142 }
110-
111- int a ;
112- for (int i = 0 ; i < 100 ; i ++) {
113- a = is .available ();
114- System .out .print (a + ", " );
115- if (a != 0 )
116- throw new RuntimeException ("Unexpected non-zero available: " + a );
117- }
118- assert !s .isClosed ();
119- assert is .read () == -1 ;
120143 }
121144 }
122145 System .out .println ("\n complete" );
123146 }
124147
125- // Verifies IOException thrown by `available`, on a closed input stream
126- // that may, or may not, have reached EOF prior to closure.
148+ /*
149+ * Verifies IOException thrown by Socket.getInputStream().available(), on a closed input stream
150+ * that may, or may not, have reached EOF prior to closure.
151+ */
127152 static void testIOEOnClosed (boolean readUntilEOF ) throws IOException {
128153 System .out .println ("testIOEOnClosed, readUntilEOF: " + readUntilEOF );
129- InetAddress addr = InetAddress .getLoopbackAddress ();
130- ServerSocket ss = new ServerSocket ();
131- ss .bind (new InetSocketAddress (addr , 0 ), 0 );
132- int port = ss .getLocalPort ();
133-
134- try (Socket s = new Socket (addr , port )) {
135- s .getOutputStream ().write (0x43 );
136- s .shutdownOutput ();
137-
138- try (Socket soc = ss .accept ()) {
139- ss .close ();
140-
141- InputStream is = soc .getInputStream ();
142- int b = is .read ();
143- assert b == 0x43 ;
144- assert !s .isClosed ();
145- if (readUntilEOF ) {
146- b = is .read ();
147- assert b == -1 ;
148- }
149- is .close ();
150- try {
151- b = is .available ();
152- throw new RuntimeException ("UNEXPECTED successful read: " + b );
153- } catch (IOException expected ) {
154- System .out .println ("caught expected IOException:" + expected );
154+ final InetAddress addr = InetAddress .getLoopbackAddress ();
155+ try (final ServerSocket ss = new ServerSocket ()) {
156+ ss .bind (new InetSocketAddress (addr , 0 ), 0 );
157+ System .out .println ("server socket bound: " + ss );
158+ final int port = ss .getLocalPort ();
159+
160+ try (final Socket s = new Socket (addr , port )) {
161+ System .out .println ("created socket: " + s );
162+ s .getOutputStream ().write (0x43 );
163+ s .shutdownOutput ();
164+
165+ try (final Socket soc = ss .accept ()) {
166+ System .out .println ("accepted socket: " + soc );
167+ ss .close ();
168+ System .out .println ("closed server socket: " + ss );
169+
170+ final InputStream is = soc .getInputStream ();
171+ int b = is .read ();
172+ assert b == 0x43 : "unexpected byte read: " + b ;
173+ assert !s .isClosed () : "socket " + s + " is unexpectedly closed" ;
174+ if (readUntilEOF ) {
175+ b = is .read ();
176+ assert b == -1 : "unexpected byte read: " + b ;
177+ }
178+ is .close ();
179+ System .out .println ("closed inputstream of socket: " + soc );
180+ try {
181+ b = is .available ();
182+ throw new RuntimeException ("UNEXPECTED successful read: " + b );
183+ } catch (IOException expected ) {
184+ System .out .println ("caught expected IOException:" + expected );
185+ }
155186 }
156187 }
157188 }
0 commit comments