KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > process > StreamCopier


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.process;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.OutputStream JavaDoc;
10
11 /**
12  * A simple thread that copies one stream to another. Useful for copying a process's output/error streams to this
13  * process's output/error streams.
14  */

15 public class StreamCopier extends Thread JavaDoc {
16
17   protected final InputStream JavaDoc in;
18   protected final OutputStream JavaDoc out;
19
20   public StreamCopier(InputStream JavaDoc stream, OutputStream JavaDoc out) {
21     if ((stream == null) || (out == null)) { throw new AssertionError JavaDoc("null streams not allowed"); }
22
23     this.in = stream;
24     this.out = out;
25     setName("Stream Copier");
26     setDaemon(true);
27   }
28
29   public void run() {
30     byte[] buf = new byte[4096];
31
32     try {
33       int read;
34       while ((read = in.read(buf)) >= 0) {
35         out.write(buf, 0, read);
36       }
37     } catch (IOException JavaDoc ioe) {
38       ioe.printStackTrace();
39     }
40   }
41
42 }
Popular Tags