Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/coders/StreamTimeout.java
38828 views
/*1* Copyright (c) 2010, 2012, 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.ServerSocket;39import java.net.Socket;4041public class StreamTimeout {42static final PrintStream log = System.err;43static String charset = "US-ASCII";4445private static class Client extends Thread implements Closeable {46private final Socket so;4748Client(int port) throws IOException {49so = new Socket("127.0.0.1", port);50}5152@Override53public void run() {54try {55Writer wr = new OutputStreamWriter(so.getOutputStream(),56charset);57wr.write("ab");58wr.flush();59} catch (IOException x) {60log.print("Unexpected exception in writer: ");61x.printStackTrace();62}63}6465@Override66public void close() throws IOException {67so.close();68}69}7071private static void gobble(InputStream is, Reader rd,72int ec, boolean force)73throws Exception74{75int a = is.available();76boolean r = rd.ready();77log.print("" + a + " bytes available, "78+ "reader " + (r ? "" : "not ") + "ready");79if (!r && !force) {80log.println();81return;82}83int c;84try {85c = rd.read();86} catch (InterruptedIOException x) {87log.println();88throw x;89}90log.println(", read() ==> "91+ (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));92if (c != ec)93throw new Exception("Incorrect value read: Expected "94+ ec + ", read " + (char)c);95}9697public static void main(String[] args) throws Exception {9899if (args.length > 0)100charset = args[0];101102try(ServerSocket ss = new ServerSocket(0);103Client cl = new Client(ss.getLocalPort())) {104105cl.start();106107try(Socket s = ss.accept()) {108s.setSoTimeout(150);109110try(InputStream is = s.getInputStream();111Reader rd = new InputStreamReader(is, charset)) {112113while (is.available() <= 0)114Thread.yield();115116gobble(is, rd, 'a', false);117gobble(is, rd, 'b', false);118gobble(is, rd, -1, false);119120boolean caught = false;121try {122gobble(is, rd, -1, true);123} catch (InterruptedIOException e) {124log.println("Read timed out, as expected");125caught = true;126}127if (!caught) {128log.println("Read did not time out, test inapplicable");129return;130}131132caught = false;133try {134gobble(is, rd, -1, true);135} catch (InterruptedIOException x) {136log.println("Second read timed out, as expected");137caught = true;138}139if (!caught)140throw new Exception("Second read completed");141}142}143144cl.join();145}146}147}148149150