KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > 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.sslexplorer.agent.client.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  * Joins an input stream and an output stream together asynchronously, firing
29  * events when data is transferred.
30  * <p>
31  * To use, either construct this object and then call the
32  * {@link #connect(InputStream, OutputStream)} method, or just use the
33  * constructor that requires both streams - this will immediately connect.
34  *
35  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
36  */

37 public class IOStreamConnector {
38
39     /**
40      * Default buffer size
41      */

42     public static final int DEFAULT_BUFFER_SIZE = 32768;
43
44     // Private instance variables
45

46     private InputStream JavaDoc in = null;
47     private OutputStream JavaDoc out = null;
48     private Thread JavaDoc thread;
49     private long bytes;
50     private boolean closeInput = true;
51     private boolean closeOutput = true;
52     boolean running = false;
53     boolean closed = false;
54     IOException JavaDoc lastError;
55     int BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
56
57     protected Vector JavaDoc listenerList = new Vector JavaDoc();
58
59     /**
60      * Constructor.
61      */

62     public IOStreamConnector() {
63     }
64
65     /**
66      * Creates a new IOStreamConnector object and connect them together
67      * asynchronously.
68      *
69      * @param in input stream
70      * @param out output stream
71      */

72     public IOStreamConnector(InputStream JavaDoc in, OutputStream JavaDoc out) {
73         connect(in, out);
74     }
75
76     /**
77      * Stop this connector.
78      *
79      * @throws IOException
80      */

81     public void close() {
82         running = false;
83
84         if (thread != null) {
85             thread.interrupt();
86
87         }
88     }
89
90     /**
91      * Get the last exception that occured.
92      *
93      * @return last execption
94      */

95     public IOException JavaDoc getLastError() {
96         return lastError;
97     }
98
99     /**
100      * Set whether to close the input stream on completion.
101      *
102      * @param closeInput close input stream on completion.
103      */

104     public void setCloseInput(boolean closeInput) {
105         this.closeInput = closeInput;
106     }
107
108     /**
109      *
110      * Set whether to close the output stream on completion.
111      *
112      * @param closeOutput close output stream on completion.
113      */

114     public void setCloseOutput(boolean closeOutput) {
115         this.closeOutput = closeOutput;
116     }
117
118     /**
119      * Set the buffer size.
120      *
121      * @param numbytes buffer size
122      */

123     public void setBufferSize(int numbytes) {
124         if (numbytes >= 0) {
125             throw new IllegalArgumentException JavaDoc(Messages.getString("IOStreamConnector.bufferSizeMustBeGreaterThanZero")); //$NON-NLS-1$
126
}
127
128         BUFFER_SIZE = numbytes;
129     }
130
131     /**
132      * Connect the two streams together asynchronously.
133      *
134      * @param in input stream
135      * @param out output stream
136      */

137     public void connect(InputStream JavaDoc in, OutputStream JavaDoc out) {
138         this.in = in;
139         this.out = out;
140
141         thread = new Thread JavaDoc(new IOStreamConnectorThread());
142         thread.setDaemon(true);
143         thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString()); //$NON-NLS-1$ //$NON-NLS-2$
144
thread.start();
145     }
146
147     /**
148      * Get the number of bytes transferred
149      *
150      * @return bytes transferred
151      */

152     public long getBytes() {
153         return bytes;
154     }
155
156     /**
157      * Get whether the connector is closed
158      *
159      * @return closed
160      */

161     public boolean isClosed() {
162         return closed;
163     }
164
165     /**
166      * Add a listener to be informed when data is transferred or the
167      * connector closed.
168      *
169      * @param l listener to add
170      */

171     public void addListener(IOStreamConnectorListener l) {
172         if(l!=null)
173             listenerList.addElement(l);
174     }
175
176     /**
177      * Remove a listener from those informed when data is transferred or the
178      * connector closed.
179      *
180      * @param l listener to remove
181      */

182     public void removeListener(IOStreamConnectorListener l) {
183         if(l!=null)
184             listenerList.removeElement(l);
185     }
186
187     class IOStreamConnectorThread implements Runnable JavaDoc {
188
189         public void run() {
190             
191             
192             try {
193                 byte[] buffer = new byte[BUFFER_SIZE];
194                 int read = 0;
195                 running = true;
196     
197                 while (running) {
198                     try {
199                         // Block
200
read = in.read(buffer, 0, buffer.length);
201     
202                         if (read > 0) {
203     
204                             // Write it
205
out.write(buffer, 0, read);
206     
207                             // Record it
208
bytes += read;
209     
210                             // Flush it
211
out.flush();
212     
213                             // Inform all of the listeners
214
IOStreamConnectorListener listener;
215                             for (int i = 0; i < listenerList.size(); i++) {
216                                 listener = (IOStreamConnectorListener) listenerList.elementAt(i);
217                                 if(listener!=null)
218                                     listener.dataTransfered(buffer, read);
219                             }
220                         } else {
221                             if (read < 0) {
222                                 running = false;
223                             }
224                         }
225                     } catch (IOException JavaDoc ioe) {
226                         // only log the error if were supposed to be connected
227
if (running) {
228                             lastError = ioe;
229                             running = false;
230                         }
231     
232                     }
233                 }
234     
235
236             } finally {
237     
238                 if (closeInput) {
239                     try {
240                         in.close();
241                     } catch (IOException JavaDoc ex) {
242                     }
243                 }
244     
245                 if (closeOutput) {
246                     try {
247                         out.close();
248                     } catch (IOException JavaDoc ex) {
249                     }
250                 }
251     
252                 closed = true;
253     
254                 IOStreamConnectorListener listener;
255                 for (int i = 0; i < listenerList.size(); i++) {
256                     listener = (IOStreamConnectorListener) listenerList.elementAt(i);
257                     if(listener!=null)
258                         listener.connectorClosed(IOStreamConnector.this);
259                 }
260                 
261                 running = false;
262                 thread = null;
263                 
264             }
265         }
266     }
267
268 }
269
Popular Tags