1 19 20 package com.sslexplorer.agent.client.util; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.Vector ; 26 27 37 public class IOStreamConnector { 38 39 42 public static final int DEFAULT_BUFFER_SIZE = 32768; 43 44 46 private InputStream in = null; 47 private OutputStream out = null; 48 private Thread 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 lastError; 55 int BUFFER_SIZE = DEFAULT_BUFFER_SIZE; 56 57 protected Vector listenerList = new Vector (); 58 59 62 public IOStreamConnector() { 63 } 64 65 72 public IOStreamConnector(InputStream in, OutputStream out) { 73 connect(in, out); 74 } 75 76 81 public void close() { 82 running = false; 83 84 if (thread != null) { 85 thread.interrupt(); 86 87 } 88 } 89 90 95 public IOException getLastError() { 96 return lastError; 97 } 98 99 104 public void setCloseInput(boolean closeInput) { 105 this.closeInput = closeInput; 106 } 107 108 114 public void setCloseOutput(boolean closeOutput) { 115 this.closeOutput = closeOutput; 116 } 117 118 123 public void setBufferSize(int numbytes) { 124 if (numbytes >= 0) { 125 throw new IllegalArgumentException (Messages.getString("IOStreamConnector.bufferSizeMustBeGreaterThanZero")); } 127 128 BUFFER_SIZE = numbytes; 129 } 130 131 137 public void connect(InputStream in, OutputStream out) { 138 this.in = in; 139 this.out = out; 140 141 thread = new Thread (new IOStreamConnectorThread()); 142 thread.setDaemon(true); 143 thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString()); thread.start(); 145 } 146 147 152 public long getBytes() { 153 return bytes; 154 } 155 156 161 public boolean isClosed() { 162 return closed; 163 } 164 165 171 public void addListener(IOStreamConnectorListener l) { 172 if(l!=null) 173 listenerList.addElement(l); 174 } 175 176 182 public void removeListener(IOStreamConnectorListener l) { 183 if(l!=null) 184 listenerList.removeElement(l); 185 } 186 187 class IOStreamConnectorThread implements Runnable { 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 read = in.read(buffer, 0, buffer.length); 201 202 if (read > 0) { 203 204 out.write(buffer, 0, read); 206 207 bytes += read; 209 210 out.flush(); 212 213 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 ioe) { 226 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 ex) { 242 } 243 } 244 245 if (closeOutput) { 246 try { 247 out.close(); 248 } catch (IOException 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 |