Path: blob/master/test/jdk/java/io/InputStreamReader/One.java
41152 views
/*1* Copyright (c) 2001, 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 440179825@summary Check that single-character reads work properly26*/272829import java.io.*;303132public class One {3334private abstract static class Test {3536InputStreamReader isr;37StringBuffer sb;38String expect;3940Test(byte[] in, String expect) throws Exception {41isr = new InputStreamReader(new ByteArrayInputStream(in), "UTF-8");42sb = new StringBuffer(expect.length());43this.expect = expect;44go();45}4647void go() throws Exception {48read();49if (!expect.equals(sb.toString()))50throw new Exception("Expected " + expect51+ ", got " + sb.toString());52}5354abstract void read() throws IOException;5556}575859private static void test(String expect) throws Exception {60byte[] in = expect.getBytes("UTF-8");6162new Test(in, expect) {63public void read() throws IOException {64for (;;) {65int c;66if ((c = isr.read()) == -1)67break;68sb.append((char)c);69}70}};7172new Test(in, expect) {73public void read() throws IOException {74for (;;) {75char[] cb = new char[1];76if (isr.read(cb) == -1)77break;78sb.append(cb[0]);79}80}};8182new Test(in, expect) {83public void read() throws IOException {84for (;;) {85char[] cb = new char[2];86int n;87if ((n = isr.read(cb)) == -1)88break;89sb.append(cb[0]);90if (n == 2)91sb.append(cb[1]);92}93}};9495}9697public static void main(String[] args) throws Exception {98test("x");99test("xy");100test("xyz");101test("\ud800\udc00");102test("x\ud800\udc00");103}104105}106107108