6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4091859 27 * @summary Test Socket.available() 28 * @run main CloseAvailable 29 * @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable 30 */ 31 32 import java.net.*; 33 import java.io.*; 34 35 36 public class CloseAvailable implements Runnable { 37 static ServerSocket ss; 38 static InetAddress addr; 39 static int port; 40 41 public static void main(String[] args) throws Exception { 42 boolean error = true; 43 addr = InetAddress.getLocalHost(); 44 ss = new ServerSocket(0); 45 port = ss.getLocalPort(); 46 47 Thread t = new Thread(new CloseAvailable()); 48 t.start(); 49 50 Socket soc = ss.accept(); 51 ss.close(); 52 53 DataInputStream is = new DataInputStream(soc.getInputStream()); 54 is.close(); 55 56 try { 57 is.available(); 58 } 59 catch (IOException ex) { 60 error = false; 61 } 62 if (error) 63 throw new RuntimeException("Available() can be called after stream closed."); 64 } 65 66 public void run() { 67 try { 68 Socket s = new Socket(addr, port); 69 s.close(); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } 73 } 74 75 } | 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4091859 8189366 27 * @summary Test Socket.available() 28 * @run main CloseAvailable 29 * @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable 30 */ 31 32 import java.net.*; 33 import java.io.*; 34 35 36 public class CloseAvailable { 37 38 public static void main(String[] args) throws Exception { 39 testClose(); 40 41 testEOF(true); 42 testEOF(false); 43 } 44 45 static void testClose() throws IOException { 46 boolean error = true; 47 InetAddress addr = InetAddress.getLocalHost(); 48 ServerSocket ss = new ServerSocket(0); 49 int port = ss.getLocalPort(); 50 51 Thread t = new Thread(new Thread("Close-Available-1") { 52 public void run() { 53 try { 54 Socket s = new Socket(addr, port); 55 s.close(); 56 } catch (Exception e) { 57 e.printStackTrace(); 58 } 59 } 60 }); 61 62 t.start(); 63 64 Socket soc = ss.accept(); 65 ss.close(); 66 67 DataInputStream is = new DataInputStream(soc.getInputStream()); 68 is.close(); 69 70 try { 71 is.available(); 72 } 73 catch (IOException ex) { 74 error = false; 75 } 76 if (error) 77 throw new RuntimeException("Available() can be called after stream closed."); 78 } 79 80 // Verifies consistency of `available` behaviour when EOF reached, both 81 // explicitly and implicitly. 82 static void testEOF(boolean readUntilEOF) throws IOException { 83 System.out.println("testEOF, readUntilEOF: " + readUntilEOF); 84 InetAddress addr = InetAddress.getLoopbackAddress(); 85 ServerSocket ss = new ServerSocket(); 86 ss.bind(new InetSocketAddress(addr, 0), 0); 87 int port = ss.getLocalPort(); 88 89 try (Socket s = new Socket(addr, port)) { 90 s.getOutputStream().write(0x42); 91 s.shutdownOutput(); 92 93 try (Socket soc = ss.accept()) { 94 ss.close(); 95 96 InputStream is = soc.getInputStream(); 97 int b = is.read(); 98 assert b == 0x42; 99 assert !s.isClosed(); 100 if (readUntilEOF) { 101 b = is.read(); 102 assert b == -1; 103 } 104 105 int a; 106 for (int i = 0; i < 100; i++) { 107 a = is.available(); 108 System.out.print(a + ", "); 109 if (a != 0) 110 throw new RuntimeException("Unexpected non-zero available: " + a); 111 } 112 assert !s.isClosed(); 113 assert is.read() == -1; 114 } 115 } 116 System.out.println("\ncomplete"); 117 } 118 } |