blob: 6754b4ec88964849753a91d1004df0baeee8e848 [file] [log] [blame]
chegar2d1ab832011-09-01 06:45:00 +01001/*
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
31import java.io.InputStream;
32import java.io.OutputStream;
33import java.net.InetAddress;
34import java.net.InetSocketAddress;
35import java.net.ServerSocket;
36import java.net.Socket;
37import java.nio.channels.ServerSocketChannel;
38import java.nio.channels.SocketChannel;
39
40public class ShutdownInput {
41 static boolean failed = false;
42
43 public static void main(String args[]) throws Exception {
44 InetAddress iaddr = InetAddress.getLocalHost();
45
46 try ( ServerSocket ss = new ServerSocket(0);
47 Socket s1 = new Socket(iaddr, ss.getLocalPort());
48 Socket s2 = ss.accept() ) {
49
50 test(s1, s2, "Testing NET");
51 }
52
53 // check the NIO socket adapter
54 try (ServerSocketChannel sc = ServerSocketChannel.open().bind(null);
55 SocketChannel s1 = SocketChannel.open(
56 new InetSocketAddress(iaddr, sc.socket().getLocalPort()));
57 SocketChannel s2 = sc.accept() ) {
58
59 test(s1.socket(), s2.socket(), "Testing NIO");
60 }
61
62 if (failed) {
63 throw new RuntimeException("Failed: check output");
64 }
65 }
66
67 public static void test(Socket s1, Socket s2, String mesg) throws Exception {
68 OutputStream os = s1.getOutputStream();
69 os.write("This is a message".getBytes("US-ASCII"));
70
71 InputStream in = s2.getInputStream();
72 s2.shutdownInput();
73
74 if (in.available() != 0) {
75 failed = true;
76 System.out.println(mesg + ":" + s2 + " in.available() should be 0, " +
77 "but returns "+ in.available());
78 }
79
80 byte[] ba = new byte[2];
81 if (in.read() != -1 ||
82 in.read(ba) != -1 ||
83 in.read(ba, 0, ba.length) != -1) {
84
85 failed = true;
86 System.out.append(mesg + ":" + s2 + " in.read() should be -1");
87 }
88 }
89}