Path: blob/master/test/jdk/java/net/Socket/CloseAvailable.java
41149 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4091859 818936626* @library /test/lib27* @summary Test Socket.available()28* @run main CloseAvailable29* @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable30*/3132import java.net.*;33import java.io.*;34import jdk.test.lib.net.IPSupport;353637public class CloseAvailable {3839public static void main(String[] args) throws Exception {40IPSupport.throwSkippedExceptionIfNonOperational();4142testClose();4344testEOF(true);45testEOF(false);46testIOEOnClosed(true);47testIOEOnClosed(false);48}4950static void testClose() throws IOException {51boolean error = true;52InetAddress addr = InetAddress.getLocalHost();53ServerSocket ss = new ServerSocket(0, 0, addr);54int port = ss.getLocalPort();5556Thread t = new Thread(new Thread("Close-Available-1") {57public void run() {58try {59Socket s = new Socket(addr, port);60s.close();61} catch (Exception e) {62e.printStackTrace();63}64}65});6667t.start();6869Socket soc = ss.accept();70ss.close();7172DataInputStream is = new DataInputStream(soc.getInputStream());73is.close();7475try {76is.available();77}78catch (IOException ex) {79error = false;80}81if (error)82throw new RuntimeException("Available() can be called after stream closed.");83}8485// Verifies consistency of `available` behaviour when EOF reached, both86// explicitly and implicitly.87static void testEOF(boolean readUntilEOF) throws IOException {88System.out.println("testEOF, readUntilEOF: " + readUntilEOF);89InetAddress addr = InetAddress.getLoopbackAddress();90ServerSocket ss = new ServerSocket();91ss.bind(new InetSocketAddress(addr, 0), 0);92int port = ss.getLocalPort();9394try (Socket s = new Socket(addr, port)) {95s.getOutputStream().write(0x42);96s.shutdownOutput();9798try (Socket soc = ss.accept()) {99ss.close();100101InputStream is = soc.getInputStream();102int b = is.read();103assert b == 0x42;104assert !s.isClosed();105if (readUntilEOF) {106b = is.read();107assert b == -1;108}109110int a;111for (int i = 0; i < 100; i++) {112a = is.available();113System.out.print(a + ", ");114if (a != 0)115throw new RuntimeException("Unexpected non-zero available: " + a);116}117assert !s.isClosed();118assert is.read() == -1;119}120}121System.out.println("\ncomplete");122}123124// Verifies IOException thrown by `available`, on a closed input stream125// that may, or may not, have reached EOF prior to closure.126static void testIOEOnClosed(boolean readUntilEOF) throws IOException {127System.out.println("testIOEOnClosed, readUntilEOF: " + readUntilEOF);128InetAddress addr = InetAddress.getLoopbackAddress();129ServerSocket ss = new ServerSocket();130ss.bind(new InetSocketAddress(addr, 0), 0);131int port = ss.getLocalPort();132133try (Socket s = new Socket(addr, port)) {134s.getOutputStream().write(0x43);135s.shutdownOutput();136137try (Socket soc = ss.accept()) {138ss.close();139140InputStream is = soc.getInputStream();141int b = is.read();142assert b == 0x43;143assert !s.isClosed();144if (readUntilEOF) {145b = is.read();146assert b == -1;147}148is.close();149try {150b = is.available();151throw new RuntimeException("UNEXPECTED successful read: " + b);152} catch (IOException expected) {153System.out.println("caught expected IOException:" + expected);154}155}156}157System.out.println("\ncomplete");158}159}160161162