KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > server > 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.applications.server;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  *
28  *
29  * @author Lee David Painter
30  */

31 public class IOStreamConnector {
32
33   private InputStream JavaDoc in = null;
34   private OutputStream JavaDoc out = null;
35   private Thread JavaDoc thread;
36   private long bytes;
37   private boolean closeInput = true;
38   private boolean closeOutput = true;
39   boolean running = false;
40   boolean closed = false;
41   IOException JavaDoc lastError;
42   public static final int DEFAULT_BUFFER_SIZE = 32768;
43   int BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
44
45   /** */
46   protected Vector JavaDoc listenerList = new Vector JavaDoc();
47
48   /**
49    * Creates a new IOStreamConnector object.
50    */

51   public IOStreamConnector() {
52   }
53
54   /**
55    * Creates a new IOStreamConnector object.
56    *
57    * @param in
58    * @param out
59    */

60   public IOStreamConnector(InputStream JavaDoc in, OutputStream JavaDoc out) {
61     connect(in, out);
62   }
63
64   /**
65    *
66    *
67    * @return
68    */

69   /* public IOStreamConnectorState getState() {
70      return state;
71    }*/

72
73   /**
74    *
75    *
76    * @throws IOException
77    */

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

96   public void setCloseInput(boolean closeInput) {
97     this.closeInput = closeInput;
98   }
99
100   /**
101    *
102    *
103    * @param closeOutput
104    */

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

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

139   public long getBytes() {
140     return bytes;
141   }
142
143   public boolean isClosed() {
144     return closed;
145   }
146
147   /**
148    *
149    *
150    * @param l
151    */

152   public void addListener(IOStreamConnectorListener l) {
153     listenerList.addElement(l);
154   }
155
156   /**
157    *
158    *
159    * @param l
160    */

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