Path: blob/master/test/jdk/java/io/charStreams/LineSink.java
41152 views
/*1* Copyright (c) 1997, 2000, 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/**/2425import java.io.*;262728class LineSink implements Runnable {2930DataInputStream ui;31BufferedReader ti;32int count;33PrintWriter log;3435public LineSink(InputStream us, BufferedReader ts,36int count, PrintWriter log)37throws IOException38{39this(us, ts, log);40this.count = count;41}4243public LineSink(InputStream us, BufferedReader ts, PrintWriter log)44throws IOException45{46ui = new DataInputStream(us);47ti = ts;48this.count = Integer.MAX_VALUE;49this.log = log;50}5152private String readUTFLine() throws IOException {53String s;54try {55s = ui.readUTF();56}57catch (EOFException x) {58return null;59}60return s;61}6263public void run() {64try {65for (int ln = 0; ln < count; ln++) {66String us = readUTFLine();67if (us == null) {68if (count < Integer.MAX_VALUE)69throw new RuntimeException("Premature EOF on UTF stream");70log.println("EOF on UTF stream");71break;72}7374String ts = ti.readLine();75if (ts == null) {76if (count < Integer.MAX_VALUE)77throw new RuntimeException("Premature EOF on char stream");78log.println("EOF on char stream");79break;80}8182if (us.length() != ts.length()) {83log.println("Length mismatch: us = \""84+ us + "\", ts = \""85+ ts + "\"");86throw new RuntimeException("Line " + ln +87": Length mismatch: " +88us.length() + " " + ts.length());89}9091for (int i = 0; i < us.length(); i++) {92if (us.charAt(i) != ts.charAt(i))93throw new RuntimeException("Line " + ln +94": Char mismatch: [" + i + "] " +95Integer.toHexString(us.charAt(i)) +96" " + Integer.toHexString(ts.charAt(i)));97}98log.println(ln + " " + ts.length());99}100101if (readUTFLine() != null)102throw new RuntimeException("Expected EOF on UTF stream");103if (ti.readLine() != null)104throw new RuntimeException("Expected EOF on char stream");105} catch (IOException x) {106throw new RuntimeException("Unexpected IOException: " + x);107}108}109110}111112113