Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/ShutdownInput.java
38812 views
/*1* Copyright (c) 2011, 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 701486026* @summary Socket.getInputStream().available() not clear for27* case that connection is shutdown for reading28*/2930import java.io.InputStream;31import java.io.OutputStream;32import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.ServerSocket;35import java.net.Socket;36import java.nio.channels.ServerSocketChannel;37import java.nio.channels.SocketChannel;3839public class ShutdownInput {40static boolean failed = false;4142public static void main(String args[]) throws Exception {43InetAddress iaddr = InetAddress.getLocalHost();4445try ( ServerSocket ss = new ServerSocket(0);46Socket s1 = new Socket(iaddr, ss.getLocalPort());47Socket s2 = ss.accept() ) {4849test(s1, s2, "Testing NET");50}5152// check the NIO socket adapter53try (ServerSocketChannel sc = ServerSocketChannel.open().bind(null);54SocketChannel s1 = SocketChannel.open(55new InetSocketAddress(iaddr, sc.socket().getLocalPort()));56SocketChannel s2 = sc.accept() ) {5758test(s1.socket(), s2.socket(), "Testing NIO");59}6061if (failed) {62throw new RuntimeException("Failed: check output");63}64}6566public static void test(Socket s1, Socket s2, String mesg) throws Exception {67OutputStream os = s1.getOutputStream();68os.write("This is a message".getBytes("US-ASCII"));6970InputStream in = s2.getInputStream();71s2.shutdownInput();7273if (in.available() != 0) {74failed = true;75System.out.println(mesg + ":" + s2 + " in.available() should be 0, " +76"but returns "+ in.available());77}7879byte[] ba = new byte[2];80if (in.read() != -1 ||81in.read(ba) != -1 ||82in.read(ba, 0, ba.length) != -1) {8384failed = true;85System.out.append(mesg + ":" + s2 + " in.read() should be -1");86}87}88}899091