Path: blob/master/test/jdk/java/nio/charset/coders/StreamTimeout.java
41153 views
/*1* Copyright (c) 2010, 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/* @test24* @bug 452194225* @summary Ensure that InputStreamReaders work properly26* when the underlying byte stream times out27*/2829import java.io.Closeable;30import java.io.IOException;31import java.io.InputStream;32import java.io.InputStreamReader;33import java.io.InterruptedIOException;34import java.io.OutputStreamWriter;35import java.io.PrintStream;36import java.io.Reader;37import java.io.Writer;38import java.net.InetAddress;39import java.net.ServerSocket;40import java.net.Socket;4142public class StreamTimeout {43static final PrintStream log = System.err;44static String charset = "US-ASCII";4546private static class Client extends Thread implements Closeable {47private final Socket so;4849Client(int port) throws IOException {50so = new Socket(InetAddress.getLoopbackAddress(), port);51}5253@Override54public void run() {55try {56Writer wr = new OutputStreamWriter(so.getOutputStream(),57charset);58wr.write("ab");59wr.flush();60} catch (IOException x) {61log.print("Unexpected exception in writer: ");62x.printStackTrace();63}64}6566@Override67public void close() throws IOException {68so.close();69}70}7172private static void gobble(InputStream is, Reader rd,73int ec, boolean force)74throws Exception75{76int a = is.available();77boolean r = rd.ready();78log.print("" + a + " bytes available, "79+ "reader " + (r ? "" : "not ") + "ready");80if (!r && !force) {81log.println();82return;83}84int c;85try {86c = rd.read();87} catch (InterruptedIOException x) {88log.println();89throw x;90}91log.println(", read() ==> "92+ (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));93if (c != ec)94throw new Exception("Incorrect value read: Expected "95+ ec + ", read " + (char)c);96}9798public static void main(String[] args) throws Exception {99100if (args.length > 0)101charset = args[0];102103try(ServerSocket ss = new ServerSocket(0);104Client cl = new Client(ss.getLocalPort())) {105106cl.start();107108try(Socket s = ss.accept()) {109s.setSoTimeout(150);110111try(InputStream is = s.getInputStream();112Reader rd = new InputStreamReader(is, charset)) {113114while (is.available() <= 0)115Thread.yield();116117gobble(is, rd, 'a', false);118gobble(is, rd, 'b', false);119gobble(is, rd, -1, false);120121boolean caught = false;122try {123gobble(is, rd, -1, true);124} catch (InterruptedIOException e) {125log.println("Read timed out, as expected");126caught = true;127}128if (!caught) {129log.println("Read did not time out, test inapplicable");130return;131}132133caught = false;134try {135gobble(is, rd, -1, true);136} catch (InterruptedIOException x) {137log.println("Second read timed out, as expected");138caught = true;139}140if (!caught)141throw new Exception("Second read completed");142}143}144145cl.join();146}147}148}149150151