KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > hook > StreamRedirectThread


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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

19 class StreamRedirectThread extends Thread JavaDoc {
20     private static final int BUFFER_SIZE = 2048;
21
22     private static final int SLEEP = 5;
23
24     private InputStream JavaDoc is;
25
26     private OutputStream JavaDoc os;
27
28     public StreamRedirectThread(String JavaDoc name, InputStream JavaDoc is, OutputStream JavaDoc 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 JavaDoc e) {
44                     ;
45                 }
46             }
47         } catch (Exception JavaDoc 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 }
Popular Tags