One.java revision 0
0N/A/*
0N/A * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/* @test
0N/A @bug 4401798
0N/A @summary Check that single-character reads work properly
0N/A */
0N/A
0N/A
0N/Aimport java.io.*;
0N/A
0N/A
0N/Apublic class One {
0N/A
0N/A private static abstract class Test {
0N/A
0N/A InputStreamReader isr;
0N/A StringBuffer sb;
0N/A String expect;
0N/A
0N/A Test(byte[] in, String expect) throws Exception {
0N/A isr = new InputStreamReader(new ByteArrayInputStream(in), "UTF-8");
0N/A sb = new StringBuffer(expect.length());
0N/A this.expect = expect;
0N/A go();
0N/A }
0N/A
0N/A void go() throws Exception {
0N/A read();
0N/A if (!expect.equals(sb.toString()))
0N/A throw new Exception("Expected " + expect
0N/A + ", got " + sb.toString());
0N/A }
0N/A
0N/A abstract void read() throws IOException;
0N/A
0N/A }
0N/A
0N/A
0N/A private static void test(String expect) throws Exception {
0N/A byte[] in = expect.getBytes("UTF-8");
0N/A
0N/A new Test(in, expect) {
0N/A public void read() throws IOException {
0N/A for (;;) {
0N/A int c;
0N/A if ((c = isr.read()) == -1)
0N/A break;
0N/A sb.append((char)c);
0N/A }
0N/A }};
0N/A
0N/A new Test(in, expect) {
0N/A public void read() throws IOException {
0N/A for (;;) {
0N/A char[] cb = new char[1];
0N/A if (isr.read(cb) == -1)
0N/A break;
0N/A sb.append(cb[0]);
0N/A }
0N/A }};
0N/A
0N/A new Test(in, expect) {
0N/A public void read() throws IOException {
0N/A for (;;) {
0N/A char[] cb = new char[2];
0N/A int n;
0N/A if ((n = isr.read(cb)) == -1)
0N/A break;
0N/A sb.append(cb[0]);
0N/A if (n == 2)
0N/A sb.append(cb[1]);
0N/A }
0N/A }};
0N/A
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A test("x");
0N/A test("xy");
0N/A test("xyz");
0N/A test("\ud800\udc00");
0N/A test("x\ud800\udc00");
0N/A }
0N/A
0N/A}