2115N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2115N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2115N/A *
2115N/A * This code is free software; you can redistribute it and/or modify it
2115N/A * under the terms of the GNU General Public License version 2 only, as
2115N/A * published by the Free Software Foundation.
2115N/A *
2115N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2115N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2115N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2115N/A * version 2 for more details (a copy is included in the LICENSE file that
2115N/A * accompanied this code).
2115N/A *
2115N/A * You should have received a copy of the GNU General Public License version
2115N/A * 2 along with this work; if not, write to the Free Software Foundation,
2115N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2115N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2115N/A */
2115N/A
2115N/A/* @test
2115N/A * @bug 4521942
2115N/A * @summary Ensure that InputStreamReaders work properly
2115N/A * when the underlying byte stream times out
2115N/A */
2115N/A
2115N/Aimport java.net.*;
2115N/Aimport java.io.*;
2115N/A
2115N/A
2115N/Apublic class StreamTimeout {
2115N/A
2115N/A private static PrintStream log = System.err;
2115N/A
2115N/A private static String charset = "US-ASCII";
2115N/A
2115N/A private static Object lock = new Object();
2115N/A private static synchronized void waitABit(int millisec) {
2115N/A synchronized(lock) {
2115N/A try {
2115N/A lock.wait(millisec);
2115N/A } catch (InterruptedException e) {
2115N/A //ignore
2115N/A }
2115N/A }
2115N/A }
2115N/A
2115N/A private static class Client extends Thread {
2115N/A public void run() {
2115N/A try {
2115N/A Socket so = new Socket("127.0.0.1", 22222);
2115N/A Writer wr = new OutputStreamWriter(so.getOutputStream(),
2115N/A charset);
2115N/A wr.write("ab");
2115N/A wr.flush();
2115N/A } catch (IOException x) {
2115N/A log.print("Unexpected exception in writer: ");
2115N/A x.printStackTrace();
2115N/A System.exit(1);
2115N/A }
2115N/A }
2115N/A }
2115N/A
2115N/A private static void gobble(InputStream is, Reader rd,
2115N/A int ec, boolean force)
2115N/A throws Exception
2115N/A {
2115N/A int a = is.available();
2115N/A boolean r = rd.ready();
2115N/A log.print("" + a + " bytes available, "
2115N/A + "reader " + (r ? "" : "not ") + "ready");
2115N/A if (!r && !force) {
2115N/A log.println();
2115N/A return;
2115N/A }
2115N/A int c;
2115N/A try {
2115N/A c = rd.read();
2115N/A } catch (InterruptedIOException x) {
2115N/A log.println();
2115N/A throw x;
2115N/A }
2115N/A log.println(", read() ==> "
2115N/A + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
2115N/A if (c != ec)
2115N/A throw new Exception("Incorrect value read: Expected "
2115N/A + ec + ", read " + (char)c);
2115N/A }
2115N/A
2115N/A public static void main(String[] args) throws Exception {
2115N/A
2115N/A if (args.length > 0)
2115N/A charset = args[0];
2115N/A
2115N/A ServerSocket ss = new ServerSocket(22222);
2115N/A Thread cl = new Client();
2115N/A cl.start();
2115N/A Socket s = ss.accept();
2115N/A s.setSoTimeout(150);
2115N/A InputStream is = s.getInputStream();
2115N/A Reader rd = new InputStreamReader(is, charset);
2115N/A
2115N/A while (is.available() <= 0)
2115N/A Thread.yield();
2115N/A
2115N/A gobble(is, rd, 'a', false);
2115N/A gobble(is, rd, 'b', false);
2115N/A gobble(is, rd, -1, false);
2115N/A
2115N/A boolean caught = false;
2115N/A try {
2115N/A gobble(is, rd, -1, true);
2115N/A } catch (InterruptedIOException e) {
2115N/A log.println("Read timed out, as expected");
2115N/A caught = true;
2115N/A }
2115N/A if (!caught) {
2115N/A log.println("Read did not time out, test inapplicable");
2115N/A return;
2115N/A }
2115N/A
2115N/A caught = false;
2115N/A try {
2115N/A gobble(is, rd, -1, true);
2115N/A } catch (InterruptedIOException x) {
2115N/A log.println("Second read timed out, as expected");
2115N/A caught = true;
2115N/A }
2115N/A if (!caught)
2115N/A throw new Exception("Second read completed");
2115N/A
2115N/A }
2115N/A
2115N/A}