1 /*
2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
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 7014860
27  * @summary Socket.getInputStream().available() not clear for
28  *          case that connection is shutdown for reading
29  */
30 
31 package test.java.net.Socket;
32 import org.testng.annotations.Test;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.net.InetAddress;
36 import java.net.InetSocketAddress;
37 import java.net.ServerSocket;
38 import java.net.Socket;
39 import java.nio.channels.ServerSocketChannel;
40 import java.nio.channels.SocketChannel;
41 
42 public class ShutdownInput {
43     static boolean failed = false;
44 
45     // Android-changed: Removed args & added @Test
46     @Test
main()47     public static void main() throws Exception {
48         InetAddress iaddr = InetAddress.getLocalHost();
49 
50         try ( ServerSocket ss = new ServerSocket(0);
51               Socket s1 = new Socket(iaddr, ss.getLocalPort());
52               Socket s2 = ss.accept() ) {
53 
54             test(s1, s2, "Testing NET");
55         }
56 
57         // check the NIO socket adapter
58         try (ServerSocketChannel sc = ServerSocketChannel.open().bind(null);
59              SocketChannel s1 = SocketChannel.open(
60                      new InetSocketAddress(iaddr, sc.socket().getLocalPort()));
61              SocketChannel s2 = sc.accept() ) {
62 
63             test(s1.socket(), s2.socket(), "Testing NIO");
64         }
65 
66         if (failed) {
67             throw new RuntimeException("Failed: check output");
68         }
69     }
70 
test(Socket s1, Socket s2, String mesg)71     public static void test(Socket s1, Socket s2, String mesg) throws Exception {
72         OutputStream os = s1.getOutputStream();
73         os.write("This is a message".getBytes("US-ASCII"));
74 
75         InputStream in = s2.getInputStream();
76         s2.shutdownInput();
77 
78         if (in.available() != 0) {
79             failed = true;
80             System.out.println(mesg + ":" + s2 + " in.available() should be 0, " +
81                                "but returns "+ in.available());
82         }
83 
84         byte[] ba = new byte[2];
85         if (in.read() != -1 ||
86             in.read(ba) != -1 ||
87             in.read(ba, 0, ba.length) != -1) {
88 
89             failed = true;
90             System.out.append(mesg + ":" + s2 + " in.read() should be -1");
91         }
92     }
93 }
94 
95