01: /**************************************************************************************
02: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package org.codehaus.aspectwerkz.hook;
08:
09: import java.io.InputStream;
10: import java.io.OutputStream;
11:
12: /**
13: * Redirects stream using an internal buffer of size 2048 Used to redirect std(in/out/err) streams of the target VM
14: * <p/>Inspired from Ant StreamPumper class, which seems better than the JPDA Sun demo
15: *
16: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
17: */
18: class StreamRedirectThread extends Thread {
19: private static final int BUFFER_SIZE = 2048;
20:
21: private static final int SLEEP = 5;
22:
23: private InputStream is;
24:
25: private OutputStream os;
26:
27: public StreamRedirectThread(String name, InputStream is,
28: OutputStream os) {
29: super (name);
30: setPriority(Thread.MAX_PRIORITY - 1);
31: this .is = is;
32: this .os = os;
33: }
34:
35: public void run() {
36: byte[] buf = new byte[BUFFER_SIZE];
37: int i;
38: try {
39: while ((i = is.read(buf)) > 0) {
40: os.write(buf, 0, i);
41: try {
42: Thread.sleep(SLEEP);
43: } catch (InterruptedException e) {
44: ;
45: }
46: }
47: } catch (Exception e) {
48: ;
49: } finally {
50: ; //notify();
51: }
52: }
53:
54: /*
55: * public StreamRedirectThread(String name, InputStream in, OutputStream out) { super(name); this.in = new
56: * InputStreamReader(in); this.out = new OutputStreamWriter(out); setPriority(Thread.MAX_PRIORITY-1); } public void
57: * run() { try { char[] cbuf = new char[BUFFER_SIZE]; int count; System.out.println("read" + this.getName()); while
58: * ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) { System.out.println("write" + this.getName()); out.write(cbuf, 0,
59: * count); out.flush(); } out.flush(); } catch (IOException e) { System.err.println("Child I/O Transfer failed - " +
60: * e); } finally { try { out.close(); in.close(); } catch(IOException e) { ; } } }
61: */
62: }
|