KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 14-May-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.maverick.multiplex;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 /**
14  *
15  *
16  * @author Lee David Painter
17  */

18 public class IOStreamConnector {
19
20   private InputStream JavaDoc in = null;
21   private OutputStream JavaDoc out = null;
22   private Thread JavaDoc thread;
23   private long bytes;
24   private boolean closeInput = true;
25   private boolean closeOutput = true;
26   boolean running = false;
27   boolean closed = false;
28   IOException JavaDoc lastError;
29   public static final int DEFAULT_BUFFER_SIZE = 32768;
30   int BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
31
32   /** */
33   protected Vector JavaDoc listenerList = new Vector JavaDoc();
34
35   /**
36    * Creates a new IOStreamConnector object.
37    */

38   public IOStreamConnector() {
39   }
40
41   /**
42    * Creates a new IOStreamConnector object.
43    *
44    * @param in
45    * @param out
46    */

47   public IOStreamConnector(InputStream JavaDoc in, OutputStream JavaDoc out) {
48     connect(in, out);
49   }
50
51   /**
52    *
53    *
54    * @return
55    */

56   /* public IOStreamConnectorState getState() {
57      return state;
58    }*/

59
60   /**
61    *
62    *
63    * @throws IOException
64    */

65   public void close() {
66     running = false;
67
68     if (thread != null) {
69       thread.interrupt();
70
71     }
72   }
73
74   public IOException JavaDoc getLastError() {
75     return lastError;
76   }
77
78   /**
79    *
80    *
81    * @param closeInput
82    */

83   public void setCloseInput(boolean closeInput) {
84     this.closeInput = closeInput;
85   }
86
87   /**
88    *
89    *
90    * @param closeOutput
91    */

92   public void setCloseOutput(boolean closeOutput) {
93     this.closeOutput = closeOutput;
94   }
95
96   public void setBufferSize(int numbytes) {
97     if (numbytes >= 0) {
98       throw new IllegalArgumentException JavaDoc(
99           "Buffer size must be greater than zero!");
100     }
101
102     BUFFER_SIZE = numbytes;
103   }
104
105   /**
106    *
107    *
108    * @param in
109    * @param out
110    */

111   public void connect(InputStream JavaDoc in, OutputStream JavaDoc out) {
112     this.in = in;
113     this.out = out;
114
115     thread = new Thread JavaDoc(new IOStreamConnectorThread());
116     thread.setDaemon(true);
117     thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString());
118     thread.start();
119   }
120
121   /**
122    *
123    *
124    * @return
125    */

126   public long getBytes() {
127     return bytes;
128   }
129
130   public boolean isClosed() {
131     return closed;
132   }
133
134   /**
135    *
136    *
137    * @param l
138    */

139   public void addListener(IOStreamConnectorListener l) {
140     listenerList.addElement(l);
141   }
142
143   /**
144    *
145    *
146    * @param l
147    */

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