One.java revision 12745:f068a4ffddd2
1/*
2 * Copyright (c) 2001, 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/* @test
25   @bug 4401798
26   @summary Check that single-character reads work properly
27 */
28
29
30import java.io.*;
31
32
33public class One {
34
35    private abstract static class Test {
36
37        InputStreamReader isr;
38        StringBuffer sb;
39        String expect;
40
41        Test(byte[] in, String expect) throws Exception {
42            isr = new InputStreamReader(new ByteArrayInputStream(in), "UTF-8");
43            sb = new StringBuffer(expect.length());
44            this.expect = expect;
45            go();
46        }
47
48        void go() throws Exception {
49            read();
50            if (!expect.equals(sb.toString()))
51                throw new Exception("Expected " + expect
52                                    + ", got " + sb.toString());
53        }
54
55        abstract void read() throws IOException;
56
57    }
58
59
60    private static void test(String expect) throws Exception {
61        byte[] in = expect.getBytes("UTF-8");
62
63        new Test(in, expect) {
64                public void read() throws IOException {
65                    for (;;) {
66                        int c;
67                        if ((c = isr.read()) == -1)
68                            break;
69                        sb.append((char)c);
70                    }
71                }};
72
73        new Test(in, expect) {
74                public void read() throws IOException {
75                    for (;;) {
76                        char[] cb = new char[1];
77                        if (isr.read(cb) == -1)
78                            break;
79                        sb.append(cb[0]);
80                    }
81                }};
82
83        new Test(in, expect) {
84                public void read() throws IOException {
85                    for (;;) {
86                        char[] cb = new char[2];
87                        int n;
88                        if ((n = isr.read(cb)) == -1)
89                            break;
90                        sb.append(cb[0]);
91                        if (n == 2)
92                            sb.append(cb[1]);
93                    }
94                }};
95
96    }
97
98    public static void main(String[] args) throws Exception {
99        test("x");
100        test("xy");
101        test("xyz");
102        test("\ud800\udc00");
103        test("x\ud800\udc00");
104    }
105
106}
107