J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /**/ |
| 25 | |
| 26 | import java.io.*; |
| 27 | import java.util.Random; |
| 28 | |
| 29 | |
| 30 | class RandomLineSource implements Runnable { |
| 31 | |
| 32 | DataOutputStream uo; |
| 33 | BufferedWriter to; |
| 34 | LineGenerator lg; |
| 35 | PrintWriter log; |
| 36 | |
| 37 | public RandomLineSource(OutputStream us, BufferedWriter ts, int limit, |
| 38 | PrintWriter log) |
| 39 | { |
| 40 | if (us != null) |
| 41 | uo = new DataOutputStream(us); |
| 42 | to = ts; |
| 43 | IntGenerator ig = new IntGenerator(); |
| 44 | lg = new LineGenerator(ig, new StringGenerator(ig), limit); |
| 45 | this.log = log; |
| 46 | } |
| 47 | |
| 48 | public RandomLineSource(OutputStream us, BufferedWriter ts, int limit) { |
| 49 | this(us, ts, limit, null); |
| 50 | } |
| 51 | |
| 52 | public RandomLineSource(BufferedWriter ts, int limit) { |
| 53 | this(null, ts, limit); |
| 54 | } |
| 55 | |
| 56 | private void flush() throws IOException { |
| 57 | if (uo != null) { |
| 58 | uo.flush(); |
| 59 | Thread.currentThread().yield(); |
| 60 | } |
| 61 | to.flush(); |
| 62 | for (int i = 0; i < 10; i++) |
| 63 | Thread.currentThread().yield(); |
| 64 | } |
| 65 | |
| 66 | private int count = 0; |
| 67 | |
| 68 | public void run() { |
| 69 | try { |
| 70 | String s; |
| 71 | |
| 72 | while ((s = lg.next()) != null) { |
| 73 | if (uo != null) |
| 74 | uo.writeUTF(s); |
| 75 | to.write(s + lg.lineTerminator); |
| 76 | flush(); |
| 77 | count++; |
| 78 | } |
| 79 | |
| 80 | if (uo != null) { |
| 81 | uo.close(); |
| 82 | Thread.currentThread().yield(); |
| 83 | } |
| 84 | to.close(); |
| 85 | Thread.currentThread().yield(); |
| 86 | } catch (IOException x) { |
| 87 | return; /* Probably pipe broken */ |
| 88 | } |
| 89 | } |
| 90 | } |