| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StreamRedirectThread |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2008, by Henrik Arro and Contributors | |
| 3 | * | |
| 4 | * This file is part of JSeq, a tool to automatically create | |
| 5 | * sequence diagrams by tracing program execution. | |
| 6 | * | |
| 7 | * See <http://jseq.sourceforge.net> for more information. | |
| 8 | * | |
| 9 | * JSeq is free software: you can redistribute it and/or modify | |
| 10 | * it under the terms of the GNU Lesser General Public License as | |
| 11 | * published by the Free Software Foundation, either version 3 of | |
| 12 | * the License, or (at your option) any later version. | |
| 13 | * | |
| 14 | * JSeq is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | * GNU Lesser General Public License for more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU Lesser General Public License | |
| 20 | * along with JSeq. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | */ | |
| 22 | ||
| 23 | package th.co.edge.jseq; | |
| 24 | ||
| 25 | import java.io.IOException; | |
| 26 | import java.io.InputStream; | |
| 27 | import java.io.InputStreamReader; | |
| 28 | import java.io.OutputStream; | |
| 29 | import java.io.OutputStreamWriter; | |
| 30 | import java.io.Reader; | |
| 31 | import java.io.Writer; | |
| 32 | ||
| 33 | /** | |
| 34 | * A <code>StreamRedirectThread</code> redirects everything from one | |
| 35 | * <code>InputStream</code> to another <code>OutputStream</code>. | |
| 36 | * | |
| 37 | * <p> | |
| 38 | * This class is used when launching and tracing a program to make sure that | |
| 39 | * what is written to <code>System.out</code> and <code>System.err</code> | |
| 40 | * shows up in the JSeq console windows. | |
| 41 | */ | |
| 42 | ||
| 43 | // TODO Add a CheckStyle or PMD rule to catch package private classes, methods | |
| 44 | // and fields without a package comment. | |
| 45 | class StreamRedirectThread extends Thread { | |
| 46 | private static final int BUFFER_SIZE = 1024; | |
| 47 | ||
| 48 | private final Reader in; | |
| 49 | private final Writer out; | |
| 50 | ||
| 51 | /** | |
| 52 | * Creates a new <code>StreamRedirectThread</code> with a given name, | |
| 53 | * redirecting input from the given <code>InputStream</code> to the given | |
| 54 | * <code>OutputStream</code>. | |
| 55 | * | |
| 56 | * @param name | |
| 57 | * the name of this <code>StreamRedirectThread</code> | |
| 58 | * | |
| 59 | * @param in | |
| 60 | * the <code>InputStream</code> to read from | |
| 61 | * | |
| 62 | * @param out | |
| 63 | * the <code>OutputStream</code> to write to | |
| 64 | */ | |
| 65 | StreamRedirectThread(String name, InputStream in, OutputStream out) { | |
| 66 | 0 | super(name); |
| 67 | 0 | this.in = new InputStreamReader(in); |
| 68 | 0 | this.out = new OutputStreamWriter(out); |
| 69 | 0 | setPriority(Thread.MAX_PRIORITY - 1); |
| 70 | 0 | } |
| 71 | ||
| 72 | /** | |
| 73 | * Executes in a separate Java thread, redirecting from an | |
| 74 | * <code>InputStream</code> to an <code>OutputStream</code>. | |
| 75 | */ | |
| 76 | @Override | |
| 77 | public void run() { | |
| 78 | try { | |
| 79 | 0 | char[] cbuf = new char[BUFFER_SIZE]; |
| 80 | int count; | |
| 81 | 0 | while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) { |
| 82 | 0 | out.write(cbuf, 0, count); |
| 83 | 0 | out.flush(); |
| 84 | } | |
| 85 | 0 | } catch (IOException exc) { |
| 86 | 0 | System.err.println("Child I/O Transfer - " + exc); |
| 87 | 0 | } |
| 88 | 0 | } |
| 89 | } |