1 19 20 package com.maverick.util; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.Vector ; 26 27 32 public class IOStreamConnector { 33 34 private InputStream in = null; 35 private OutputStream out = null; 36 private Thread thread; 37 private long bytes; 38 private boolean closeInput = true; 39 private boolean closeOutput = true; 40 boolean running = false; 41 boolean closed = false; 42 IOException lastError; 43 public static final int DEFAULT_BUFFER_SIZE = 32768; 44 int BUFFER_SIZE = DEFAULT_BUFFER_SIZE; 45 46 org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(IOStreamConnector.class); 48 50 51 protected Vector listenerList = new Vector (); 52 53 56 public IOStreamConnector() { 57 } 58 59 65 public IOStreamConnector(InputStream in, OutputStream out) { 66 connect(in, out); 67 } 68 69 74 77 78 83 public void close() { 84 running = false; 85 86 if (thread != null) { 87 thread.interrupt(); 88 89 } 90 } 91 92 public IOException getLastError() { 93 return lastError; 94 } 95 96 101 public void setCloseInput(boolean closeInput) { 102 this.closeInput = closeInput; 103 } 104 105 110 public void setCloseOutput(boolean closeOutput) { 111 this.closeOutput = closeOutput; 112 } 113 114 public void setBufferSize(int numbytes) { 115 if (numbytes >= 0) { 116 throw new IllegalArgumentException ( 117 "Buffer size must be greater than zero!"); 118 } 119 120 BUFFER_SIZE = numbytes; 121 } 122 123 129 public void connect(InputStream in, OutputStream out) { 130 this.in = in; 131 this.out = out; 132 133 thread = new Thread (new IOStreamConnectorThread()); 134 thread.setDaemon(true); 135 thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString()); 136 thread.start(); 137 } 138 139 144 public long getBytes() { 145 return bytes; 146 } 147 148 public boolean isClosed() { 149 return closed; 150 } 151 152 157 public void addListener(IOStreamConnectorListener l) { 158 listenerList.addElement(l); 159 } 160 161 166 public void removeListener(IOStreamConnectorListener l) { 167 listenerList.removeElement(l); 168 } 169 170 class IOStreamConnectorThread 171 implements Runnable { 172 173 public void run() { 174 byte[] buffer = new byte[BUFFER_SIZE]; 175 int read = 0; 176 running = true; 177 178 while (running) { 179 try { 180 read = in.read(buffer, 0, buffer.length); 182 183 if (read > 0) { 184 185 out.write(buffer, 0, read); 187 188 bytes += read; 190 191 out.flush(); 193 194 for (int i = 0; i < listenerList.size(); i++) { 196 ( (IOStreamConnectorListener) listenerList.elementAt(i)). 197 dataTransfered(buffer, read); 198 } 199 } 200 else { 201 if (read < 0) { 202 log.info("Received EOF from InputStream " + in.toString()); 204 running = false; 206 } 207 } 208 } 209 catch (IOException ioe) { 210 if (running) { 212 lastError = ioe; 213 running = false; 214 log.info("Error from InputStream", ioe); 216 } 218 219 } 220 } 221 222 if (closeInput) { 223 try { 224 in.close(); 225 } 226 catch (IOException ex) {} 227 } 228 229 if (closeOutput) { 230 try { 231 out.close(); 232 } 233 catch (IOException ex) {} 234 } 235 236 closed = true; 237 238 for (int i = 0; i < listenerList.size(); i++) { 239 ( (IOStreamConnectorListener) listenerList.elementAt(i)). 240 connectorClosed( 241 IOStreamConnector.this); 242 } 243 244 thread = null; 245 246 } 247 } 248 249 public interface IOStreamConnectorListener { 250 public void connectorClosed(IOStreamConnector connector); 251 252 public void dataTransfered(byte[] data, int count); 253 } 254 255 } 256 | Popular Tags |