1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.IOException;
27 
28 
29 /**
30  * A thread which pumps information read from a given input stream into the
31  * given output stream.
32  */
33 
34 public class PumpThread extends Thread{
35 
36 
37   /**
38    * The InputStream.
39    */
40 
41   private final InputStream in;
42 
43 
44 
45   /**
46    * The OutputStream.
47    */
48 
49   private final OutputStream out;
50 
51 
52 
53 
54   /**
55    * The buffer we're using.
56    */
57 
58   private final byte [] buffer;
59 
60 
61 
62 
63   /**
64    * The IOException thrown while reading or writing information, or null if
65    * none.
66    */
67 
68   private IOException exception;
69 
70 
71 
72 
73   /**
74    * Creates a new PumpThread which will pump information from the given
75    * InputStream into the given OutputStream.
76    */
77 
PumpThread(InputStream in, OutputStream out)78   public PumpThread(InputStream in, OutputStream out){
79     this(in, out, 2048);
80   }
81 
82 
83 
84 
85   /**
86    * Creates a new PumpThread which will pump information from the given
87    * InputStream into the given OutputStream and will use a buffer of the given
88    * size.
89    */
90 
PumpThread(InputStream in, OutputStream out, int bufSize)91   public PumpThread(InputStream in, OutputStream out, int bufSize){
92     this(in, out, new byte[bufSize]);
93   }
94 
95 
96 
97 
98   /**
99    * Creates a new PumpThread which will pump information from the given
100    * InputStream into the given OutputStream and will use the given buffer.
101    */
102 
PumpThread(InputStream in, OutputStream out, byte [] buffer)103   public PumpThread(InputStream in, OutputStream out, byte [] buffer){
104     this.in = in;
105     this.out = out;
106     this.buffer = buffer;
107   }
108 
109 
110 
111 
112   /**
113    * Does the actual pumping.
114    */
115 
run()116   public void run(){
117     try{
118       while (true){
119         int count = in.read(buffer);
120         if (count <= 0)
121           return;
122         out.write(buffer, 0, count);
123       }
124     } catch (IOException e){
125         exception = e;
126       }
127   }
128 
129 
130 
131 
132   /**
133    * Returns the exception thrown while reading or writing, or <code>null</code>
134    * if it finished normally, without throwing an exception (read returned -1).
135    *
136    * @throws IllegalStateException if the thread is still alive.
137    */
138 
getException()139   public IOException getException(){
140     if (isAlive())
141       throw new IllegalStateException("The thread is still alive");
142 
143     return exception;
144   }
145 
146 }
147