Path: blob/master/test/jdk/java/io/charStreams/LineNumbers.java
41149 views
/*1* Copyright (c) 1997, 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@summary Stochastic test of LineNumberReader25*/2627import java.io.*;282930public class LineNumbers {3132static String enc = "UTF8";33static String inEnc;34static String outEnc;35static int limit = 500;3637public static void main(String[] args) throws Exception {38PrintWriter log39= new PrintWriter(new OutputStreamWriter(System.err), true);4041if (inEnc == null)42inEnc = enc;43if (outEnc == null)44outEnc = enc;4546PipedOutputStream co = new PipedOutputStream();47PipedInputStream ci = new PipedInputStream(co);4849BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc));50LineNumberReader r = new LineNumberReader(new InputStreamReader(ci, inEnc));5152Thread t1 = new Thread(new RandomLineSource(w, limit));53Thread t2 = new Thread(new LineNumberSink(r, limit, log));54t1.start();55t2.start();56t1.join();57t2.join();58}5960}616263class LineNumberSink implements Runnable {6465LineNumberReader r;66int limit;67PrintWriter log;6869LineNumberSink(LineNumberReader r, int limit, PrintWriter log) {70this.r = r;71this.limit = limit;72this.log = log;73}7475public void run() {76String s;77int n = 0;7879try {80while ((s = r.readLine()) != null) {81n++;82int ln = r.getLineNumber();83log.println("[" + ln + "] " + s.length());84log.println(s);85if (log.checkError())86log.println("Conversion errors"); /* #### */87if (n != ln)88throw new RuntimeException("Line number mismatch: Expected " + n + ", got " + ln);89}90if (n != limit)91throw new RuntimeException("Incorrect line count");92}93catch (IOException x) {94throw new RuntimeException(x.toString());95}96log.println(n + " lines read");97}9899}100101102