KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > hook > StreamRedirectThread


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

4 package com.tc.aspectwerkz.hook;
5
6 import java.io.InputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 /**
10  * Redirects stream using an internal buffer of size 2048 Used to redirect std(in/out/err) streams of the target VM
11  * <p/>Inspired from Ant StreamPumper class, which seems better than the JPDA Sun demo
12  *
13  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
14  */

15 class StreamRedirectThread extends Thread JavaDoc {
16   private static final int BUFFER_SIZE = 2048;
17
18   private static final int SLEEP = 5;
19
20   private InputStream JavaDoc is;
21
22   private OutputStream JavaDoc os;
23
24   public StreamRedirectThread(String JavaDoc name, InputStream JavaDoc is, OutputStream JavaDoc os) {
25     super(name);
26     setPriority(Thread.MAX_PRIORITY - 1);
27     this.is = is;
28     this.os = os;
29   }
30
31   public void run() {
32     byte[] buf = new byte[BUFFER_SIZE];
33     int i;
34     try {
35       while ((i = is.read(buf)) > 0) {
36         os.write(buf, 0, i);
37         try {
38           Thread.sleep(SLEEP);
39         } catch (InterruptedException JavaDoc e) {
40           ;
41         }
42       }
43     } catch (Exception JavaDoc e) {
44       ;
45     } finally {
46       ; //notify();
47
}
48   }
49
50   /*
51   * public StreamRedirectThread(String name, InputStream in, OutputStream out) { super(name); this.in = new
52   * InputStreamReader(in); this.out = new OutputStreamWriter(out); setPriority(Thread.MAX_PRIORITY-1); } public void
53   * run() { try { char[] cbuf = new char[BUFFER_SIZE]; int count; System.out.println("read" + this.getName()); while
54   * ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) { System.out.println("write" + this.getName()); out.write(cbuf, 0,
55   * count); out.flush(); } out.flush(); } catch (IOException e) { System.err.println("Child I/O Transfer failed - " +
56   * e); } finally { try { out.close(); in.close(); } catch(IOException e) { ; } } }
57   */

58 }
Popular Tags