KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > thread > v1 > IOThreadRunner


1 /*
2  * @(#)IOThreadRunner.java 1.0.0 11/17/2000 - 13:41:07
3  *
4  * Copyright (C) 2000,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.thread.v1;
28
29
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33
34
35 /**
36  * Loops, reading from the input stream and writes to the output stream.
37  *
38  *
39  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40  * @since November 17, 2000
41  * @version $Date: 2003/02/10 22:52:48 $
42  * @see LoopThread
43  */

44 public class IOThreadRunner
45 {
46     private static final int BUFFER_SIZE = 4096;
47     private InputStream JavaDoc is;
48     private OutputStream JavaDoc os;
49     private byte[] buffer = new byte[ BUFFER_SIZE ];
50     private LoopThread lt;
51     private IOException JavaDoc exception;
52     private boolean closeInputOnEOF = false;
53     private boolean closeOutputOnEOF = false;
54     
55     private Runnable JavaDoc ltRunner = new Runnable JavaDoc()
56         {
57             public void run()
58             {
59                 try
60                 {
61                     int read = is.read( buffer, 0, BUFFER_SIZE );
62                     if (read > 0)
63                     {
64 /*
65 System.out.print("[IOThreadRunner: read: {");
66 System.out.write( buffer, 0, read );
67 System.out.println("} ]");
68 */

69                         os.write( buffer, 0, read );
70                     }
71                     else
72                     {
73 //System.out.print("[IOThreadRunner: EOF]");
74
reachedEOF();
75                     }
76                 }
77                 catch (IOException JavaDoc e)
78                 {
79                     registerException( e );
80                 }
81             }
82         };
83     
84     /**
85      * Create a new ThreadRunner, re-routing <tt>is</tt> data into the
86      * <tt>os</tt> stream.
87      */

88     public IOThreadRunner( InputStream JavaDoc is, OutputStream JavaDoc os )
89     {
90         this( new LoopThread(), is, os );
91         
92         this.lt.setDaemon( true );
93         this.lt.setSleepTimeMillis( 0L );
94     }
95     
96     
97     /**
98      * Create a new ThreadRunner, re-routing <tt>is</tt> data into the
99      * <tt>os</tt> stream, but uses the initialization of the given
100      * LoopThread. Note that <tt>lt</tt> must not be a running thread.
101      */

102     public IOThreadRunner( LoopThread lt, InputStream JavaDoc is, OutputStream JavaDoc os )
103     {
104         this.lt = lt;
105         lt.setRunnable( ltRunner );
106         this.is = is;
107         this.os = os;
108     }
109     
110     
111     /**
112      * Retrieves the most recent exception that occured, if any. If no
113      * exception happened, then it returns <tt>null</tt>.
114      */

115     public IOException JavaDoc getException()
116     {
117         return this.exception;
118     }
119     
120     
121     /**
122      * By setting this to <tt>true</tt> the runner will close the InputStream
123      * once a Stop signal has been encountered.
124      */

125     public void setCloseInputOnStop( boolean on )
126     {
127         this.closeInputOnEOF = on;
128     }
129     
130     
131     /**
132      * By setting this to <tt>true</tt> the runner will close the OutputStream
133      * once a Stop signal has been encountered.
134      */

135     public void setCloseOutputOnStop( boolean on )
136     {
137         this.closeOutputOnEOF = on;
138     }
139     
140     
141     /**
142      * Retrieves the LoopThread instance that manages the operation. You
143      * can use this instance for setting up the thread (such as setting
144      * priority and daemon status).
145      */

146     public LoopThread getThread()
147     {
148         return this.lt;
149     }
150     
151     
152     /**
153      * Retrieves the output stream that the input is redirected to.
154      */

155     public OutputStream JavaDoc getOutputStream()
156     {
157         return this.os;
158     }
159     
160     
161     /**
162      * @return <tt>true</tt> if the thread is alive and reading the
163      * stream, or <tt>false</tt> if it is not reading.
164      */

165     public boolean isReading()
166     {
167         return this.lt.isAlive();
168     }
169     
170     
171     /**
172      * Starts the stream reading.
173      */

174     public void start()
175     {
176         this.lt.start();
177     }
178     
179     
180     /**
181      * Stops the thread from reading.
182      */

183     public void stop()
184             throws IOException JavaDoc
185     {
186         if (this.closeInputOnEOF)
187         {
188             this.is.close();
189         }
190         if (this.closeOutputOnEOF)
191         {
192             this.os.close();
193         }
194         this.lt.stop();
195     }
196     
197     
198     /**
199      * Post an exception, and stop the reading.
200      */

201     protected void registerException( IOException JavaDoc ioe )
202     {
203         this.exception = ioe;
204         try
205         {
206             stop();
207         }
208         catch (IOException JavaDoc ex)
209         {
210             // ignore
211
}
212     }
213     
214     
215     /**
216      * Stop the reading and void out any exceptions.
217      */

218     protected void reachedEOF()
219     {
220         this.exception = null;
221         try
222         {
223             stop();
224         }
225         catch (IOException JavaDoc ioe)
226         {
227             this.exception = ioe;
228         }
229     }
230 }
231
Popular Tags