Path: blob/master/test/jdk/java/io/charStreams/RandomLineSource.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/**/2425import java.io.*;26import java.util.Random;272829class RandomLineSource implements Runnable {3031DataOutputStream uo;32BufferedWriter to;33LineGenerator lg;34PrintWriter log;3536public RandomLineSource(OutputStream us, BufferedWriter ts, int limit,37PrintWriter log)38{39if (us != null)40uo = new DataOutputStream(us);41to = ts;42IntGenerator ig = new IntGenerator();43lg = new LineGenerator(ig, new StringGenerator(ig), limit);44this.log = log;45}4647public RandomLineSource(OutputStream us, BufferedWriter ts, int limit) {48this(us, ts, limit, null);49}5051public RandomLineSource(BufferedWriter ts, int limit) {52this(null, ts, limit);53}5455private void flush() throws IOException {56if (uo != null) {57uo.flush();58Thread.currentThread().yield();59}60to.flush();61for (int i = 0; i < 10; i++)62Thread.currentThread().yield();63}6465private int count = 0;6667public void run() {68try {69String s;7071while ((s = lg.next()) != null) {72if (uo != null)73uo.writeUTF(s);74to.write(s + lg.lineTerminator);75flush();76count++;77}7879if (uo != null) {80uo.close();81Thread.currentThread().yield();82}83to.close();84Thread.currentThread().yield();85} catch (IOException x) {86return; /* Probably pipe broken */87}88}89}909192