01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.aspectwerkz.hook;
05:
06: import java.io.InputStream;
07: import java.io.OutputStream;
08:
09: /**
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 {
16: private static final int BUFFER_SIZE = 2048;
17:
18: private static final int SLEEP = 5;
19:
20: private InputStream is;
21:
22: private OutputStream os;
23:
24: public StreamRedirectThread(String name, InputStream is,
25: OutputStream os) {
26: super (name);
27: setPriority(Thread.MAX_PRIORITY - 1);
28: this .is = is;
29: this .os = os;
30: }
31:
32: public void run() {
33: byte[] buf = new byte[BUFFER_SIZE];
34: int i;
35: try {
36: while ((i = is.read(buf)) > 0) {
37: os.write(buf, 0, i);
38: try {
39: Thread.sleep(SLEEP);
40: } catch (InterruptedException e) {
41: ;
42: }
43: }
44: } catch (Exception e) {
45: ;
46: } finally {
47: ; //notify();
48: }
49: }
50:
51: /*
52: * public StreamRedirectThread(String name, InputStream in, OutputStream out) { super(name); this.in = new
53: * InputStreamReader(in); this.out = new OutputStreamWriter(out); setPriority(Thread.MAX_PRIORITY-1); } public void
54: * run() { try { char[] cbuf = new char[BUFFER_SIZE]; int count; System.out.println("read" + this.getName()); while
55: * ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) { System.out.println("write" + this.getName()); out.write(cbuf, 0,
56: * count); out.flush(); } out.flush(); } catch (IOException e) { System.err.println("Child I/O Transfer failed - " +
57: * e); } finally { try { out.close(); in.close(); } catch(IOException e) { ; } } }
58: */
59: }
|