jdk/test/java/nio/charset/coders/StreamTimeout.java
author katleman
Thu, 05 Jan 2012 08:42:37 -0800
changeset 11376 075fe3928b7f
parent 5506 202f599c92aa
child 12441 dc1628ee8f30
permissions -rw-r--r--
Added tag jdk8-b20 for changeset 90e14ec89395
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     4
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     8
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    13
 * accompanied this code).
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    14
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    21
 * questions.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    22
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    23
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    24
/* @test
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    25
 * @bug 4521942
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    26
 * @summary Ensure that InputStreamReaders work properly
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    27
 *          when the underlying byte stream times out
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    28
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    29
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    30
import java.net.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    31
import java.io.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    32
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    33
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    34
public class StreamTimeout {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    35
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    36
    private static PrintStream log = System.err;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    37
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    38
    private static String charset = "US-ASCII";
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    39
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    40
    private static Object lock = new Object();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    41
    private static synchronized void waitABit(int millisec) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    42
        synchronized(lock) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    43
            try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    44
                lock.wait(millisec);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    45
            } catch (InterruptedException e) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    46
                //ignore
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    47
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    48
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    49
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    50
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    51
    private static class Client extends Thread {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    52
        public void run() {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    53
            try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    54
                Socket so = new Socket("127.0.0.1", 22222);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    55
                Writer wr = new OutputStreamWriter(so.getOutputStream(),
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    56
                                                   charset);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    57
                wr.write("ab");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    58
                wr.flush();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    59
            } catch (IOException x) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    60
                log.print("Unexpected exception in writer: ");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    61
                x.printStackTrace();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    62
                System.exit(1);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    63
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    64
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    65
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    66
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    67
    private static void gobble(InputStream is, Reader rd,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    68
                               int ec, boolean force)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    69
        throws Exception
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    70
    {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    71
        int a = is.available();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    72
        boolean r = rd.ready();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    73
        log.print("" + a + " bytes available, "
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    74
                  + "reader " + (r ? "" : "not ") + "ready");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    75
        if (!r && !force) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    76
            log.println();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    77
            return;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    78
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    79
        int c;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    80
        try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    81
            c = rd.read();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    82
        } catch (InterruptedIOException x) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    83
            log.println();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    84
            throw x;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    85
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    86
        log.println(", read() ==> "
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    87
                    + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    88
        if (c != ec)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    89
            throw new Exception("Incorrect value read: Expected "
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    90
                                + ec + ", read " + (char)c);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    91
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    92
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    93
    public static void main(String[] args) throws Exception {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    94
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    95
        if (args.length > 0)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    96
            charset = args[0];
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    97
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    98
        ServerSocket ss = new ServerSocket(22222);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    99
        Thread cl = new Client();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   100
        cl.start();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   101
        Socket s = ss.accept();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   102
        s.setSoTimeout(150);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   103
        InputStream is = s.getInputStream();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   104
        Reader rd = new InputStreamReader(is, charset);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   105
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   106
        while (is.available() <= 0)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   107
            Thread.yield();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   108
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   109
        gobble(is, rd, 'a', false);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   110
        gobble(is, rd, 'b', false);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   111
        gobble(is, rd, -1, false);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   112
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   113
        boolean caught = false;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   114
        try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   115
            gobble(is, rd, -1, true);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   116
        } catch (InterruptedIOException e) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   117
            log.println("Read timed out, as expected");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   118
            caught = true;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   119
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   120
        if (!caught) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   121
            log.println("Read did not time out, test inapplicable");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   122
            return;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   123
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   124
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   125
        caught = false;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   126
        try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   127
            gobble(is, rd, -1, true);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   128
        } catch (InterruptedIOException x) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   129
            log.println("Second read timed out, as expected");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   130
            caught = true;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   131
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   132
        if (!caught)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   133
            throw new Exception("Second read completed");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   134
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   135
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   136
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   137
}