KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > util > IOStreamConnector


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.util;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  *
29  *
30  * @author Lee David Painter
31  */

32 public class IOStreamConnector {
33
34   private InputStream JavaDoc in = null;
35   private OutputStream JavaDoc out = null;
36   private Thread JavaDoc thread;
37   private long bytes;
38   private boolean closeInput = true;
39   private boolean closeOutput = true;
40   boolean running = false;
41   boolean closed = false;
42   IOException JavaDoc lastError;
43   public static final int DEFAULT_BUFFER_SIZE = 32768;
44   int BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
45
46   //#ifdef DEBUG
47
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(IOStreamConnector.class);
48   //#endif
49

50   /** */
51   protected Vector JavaDoc listenerList = new Vector JavaDoc();
52
53   /**
54    * Creates a new IOStreamConnector object.
55    */

56   public IOStreamConnector() {
57   }
58
59   /**
60    * Creates a new IOStreamConnector object.
61    *
62    * @param in
63    * @param out
64    */

65   public IOStreamConnector(InputStream JavaDoc in, OutputStream JavaDoc out) {
66     connect(in, out);
67   }
68
69   /**
70    *
71    *
72    * @return
73    */

74   /* public IOStreamConnectorState getState() {
75      return state;
76    }*/

77
78   /**
79    *
80    *
81    * @throws IOException
82    */

83   public void close() {
84     running = false;
85
86     if (thread != null) {
87       thread.interrupt();
88
89     }
90   }
91
92   public IOException JavaDoc getLastError() {
93     return lastError;
94   }
95
96   /**
97    *
98    *
99    * @param closeInput
100    */

101   public void setCloseInput(boolean closeInput) {
102     this.closeInput = closeInput;
103   }
104
105   /**
106    *
107    *
108    * @param closeOutput
109    */

110   public void setCloseOutput(boolean closeOutput) {
111     this.closeOutput = closeOutput;
112   }
113
114   public void setBufferSize(int numbytes) {
115     if (numbytes >= 0) {
116       throw new IllegalArgumentException JavaDoc(
117           "Buffer size must be greater than zero!");
118     }
119
120     BUFFER_SIZE = numbytes;
121   }
122
123   /**
124    *
125    *
126    * @param in
127    * @param out
128    */

129   public void connect(InputStream JavaDoc in, OutputStream JavaDoc out) {
130     this.in = in;
131     this.out = out;
132
133     thread = new Thread JavaDoc(new IOStreamConnectorThread());
134     thread.setDaemon(true);
135     thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString());
136     thread.start();
137   }
138
139   /**
140    *
141    *
142    * @return
143    */

144   public long getBytes() {
145     return bytes;
146   }
147
148   public boolean isClosed() {
149     return closed;
150   }
151
152   /**
153    *
154    *
155    * @param l
156    */

157   public void addListener(IOStreamConnectorListener l) {
158     listenerList.addElement(l);
159   }
160
161   /**
162    *
163    *
164    * @param l
165    */

166   public void removeListener(IOStreamConnectorListener l) {
167     listenerList.removeElement(l);
168   }
169
170   class IOStreamConnectorThread
171       implements Runnable JavaDoc {
172
173     public void run() {
174       byte[] buffer = new byte[BUFFER_SIZE];
175       int read = 0;
176       running = true;
177
178       while (running) {
179         try {
180           // Block
181
read = in.read(buffer, 0, buffer.length);
182
183           if (read > 0) {
184
185             // Write it
186
out.write(buffer, 0, read);
187
188             // Record it
189
bytes += read;
190
191             // Flush it
192
out.flush();
193
194             // Inform all of the listeners
195
for (int i = 0; i < listenerList.size(); i++) {
196               ( (IOStreamConnectorListener) listenerList.elementAt(i)).
197                   dataTransfered(buffer, read);
198             }
199           }
200           else {
201             if (read < 0) {
202               //#ifdef DEBUG
203
log.info("Received EOF from InputStream " + in.toString());
204               //#endif
205
running = false;
206             }
207           }
208         }
209         catch (IOException JavaDoc ioe) {
210           // only log the error if were supposed to be connected
211
if (running) {
212             lastError = ioe;
213             running = false;
214             //#ifdef DEBUG
215
log.info("Error from InputStream", ioe);
216             //#endif
217
}
218
219         }
220       }
221
222       if (closeInput) {
223         try {
224           in.close();
225         }
226         catch (IOException JavaDoc ex) {}
227       }
228
229       if (closeOutput) {
230         try {
231           out.close();
232         }
233         catch (IOException JavaDoc ex) {}
234       }
235
236       closed = true;
237
238       for (int i = 0; i < listenerList.size(); i++) {
239         ( (IOStreamConnectorListener) listenerList.elementAt(i)).
240             connectorClosed(
241             IOStreamConnector.this);
242       }
243
244       thread = null;
245
246     }
247   }
248
249   public interface IOStreamConnectorListener {
250     public void connectorClosed(IOStreamConnector connector);
251
252     public void dataTransfered(byte[] data, int count);
253   }
254
255 }
256
Popular Tags