1 19 20 package com.sslexplorer.applications.server; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.util.Vector ; 25 26 31 public class IOStreamConnector { 32 33 private InputStream in = null; 34 private OutputStream out = null; 35 private Thread 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 lastError; 42 public static final int DEFAULT_BUFFER_SIZE = 32768; 43 int BUFFER_SIZE = DEFAULT_BUFFER_SIZE; 44 45 46 protected Vector listenerList = new Vector (); 47 48 51 public IOStreamConnector() { 52 } 53 54 60 public IOStreamConnector(InputStream in, OutputStream out) { 61 connect(in, out); 62 } 63 64 69 72 73 78 public void close() { 79 running = false; 80 81 if (thread != null) { 82 thread.interrupt(); 83 84 } 85 } 86 87 public IOException getLastError() { 88 return lastError; 89 } 90 91 96 public void setCloseInput(boolean closeInput) { 97 this.closeInput = closeInput; 98 } 99 100 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 ( 112 "Buffer size must be greater than zero!"); 113 } 114 115 BUFFER_SIZE = numbytes; 116 } 117 118 124 public void connect(InputStream in, OutputStream out) { 125 this.in = in; 126 this.out = out; 127 128 thread = new Thread (new IOStreamConnectorThread()); 129 thread.setDaemon(true); 130 thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString()); 131 thread.start(); 132 } 133 134 139 public long getBytes() { 140 return bytes; 141 } 142 143 public boolean isClosed() { 144 return closed; 145 } 146 147 152 public void addListener(IOStreamConnectorListener l) { 153 listenerList.addElement(l); 154 } 155 156 161 public void removeListener(IOStreamConnectorListener l) { 162 listenerList.removeElement(l); 163 } 164 165 class IOStreamConnectorThread 166 implements Runnable { 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 read = in.read(buffer, 0, buffer.length); 177 178 if (read > 0) { 179 180 out.write(buffer, 0, read); 182 183 bytes += read; 185 186 out.flush(); 188 189 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 ioe) { 202 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 ex) {} 216 } 217 218 if (closeOutput) { 219 try { 220 out.close(); 221 } 222 catch (IOException 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 |