KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > StreamRedirectThread


1 package edu.rice.cs.util;
2
3 import java.io.*;
4
5 /** StreamRedirectThread is a thread which copies its input to its output and terminates when it completes. */
6 public class StreamRedirectThread extends Thread JavaDoc {
7     /// Input reader
8
private final Reader in;
9
10     /// Output writer
11
private final Writer out;
12
13     /// Data buffer size
14
private static final int BUFFER_SIZE = 2048;
15
16     /**
17      * Constructor
18      *
19      * @param name thread name
20      * @param in stream to copy from
21      * @param out stream to copy to
22      */

23     public StreamRedirectThread(String JavaDoc name, InputStream in, OutputStream out) {
24         super(name);
25         this.in = new InputStreamReader(in);
26         this.out = new OutputStreamWriter(out);
27         setPriority(Thread.MAX_PRIORITY - 1);
28     }
29
30     /**
31      * Copy.
32      */

33     public void run() {
34         try {
35             char[] cbuf = new char[BUFFER_SIZE];
36             int count;
37             while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) {
38                 out.write(cbuf, 0, count);
39                 out.flush();
40             }
41             out.flush();
42         }
43         catch (IOException exc) {
44           // ignore
45
}
46     }
47 }
48
Popular Tags