KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > trace > StreamRedirectThread


1 /*
2  * @(#)StreamRedirectThread.java 1.6 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-2001 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34 package com.sun.tools.example.trace;
35
36 import java.io.*;
37
38 /**
39  * StreamRedirectThread is a thread which copies it's input to
40  * it's output and terminates when it completes.
41  *
42  * @version @(#) StreamRedirectThread.java 1.6 05/11/17 00:25:20
43  * @author Robert Field
44  */

45 class StreamRedirectThread extends Thread JavaDoc {
46
47     private final Reader in;
48     private final Writer out;
49
50     private static final int BUFFER_SIZE = 2048;
51
52     /**
53      * Set up for copy.
54      * @param name Name of the thread
55      * @param in Stream to copy from
56      * @param out Stream to copy to
57      */

58     StreamRedirectThread(String JavaDoc name, InputStream in, OutputStream out) {
59     super(name);
60     this.in = new InputStreamReader(in);
61     this.out = new OutputStreamWriter(out);
62     setPriority(Thread.MAX_PRIORITY-1);
63     }
64
65     /**
66      * Copy.
67      */

68     public void run() {
69         try {
70         char[] cbuf = new char[BUFFER_SIZE];
71         int count;
72         while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) {
73         out.write(cbuf, 0, count);
74         }
75             out.flush();
76     } catch(IOException exc) {
77         System.err.println("Child I/O Transfer - " + exc);
78     }
79     }
80 }
81
Popular Tags