1 7 package com.maverick.multiplex; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.OutputStream ; 11 import java.util.Vector ; 12 13 18 public class IOStreamConnector { 19 20 private InputStream in = null; 21 private OutputStream out = null; 22 private Thread 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 lastError; 29 public static final int DEFAULT_BUFFER_SIZE = 32768; 30 int BUFFER_SIZE = DEFAULT_BUFFER_SIZE; 31 32 33 protected Vector listenerList = new Vector (); 34 35 38 public IOStreamConnector() { 39 } 40 41 47 public IOStreamConnector(InputStream in, OutputStream out) { 48 connect(in, out); 49 } 50 51 56 59 60 65 public void close() { 66 running = false; 67 68 if (thread != null) { 69 thread.interrupt(); 70 71 } 72 } 73 74 public IOException getLastError() { 75 return lastError; 76 } 77 78 83 public void setCloseInput(boolean closeInput) { 84 this.closeInput = closeInput; 85 } 86 87 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 ( 99 "Buffer size must be greater than zero!"); 100 } 101 102 BUFFER_SIZE = numbytes; 103 } 104 105 111 public void connect(InputStream in, OutputStream out) { 112 this.in = in; 113 this.out = out; 114 115 thread = new Thread (new IOStreamConnectorThread()); 116 thread.setDaemon(true); 117 thread.setName("IOStreamConnector " + in.toString() + ">>" + out.toString()); 118 thread.start(); 119 } 120 121 126 public long getBytes() { 127 return bytes; 128 } 129 130 public boolean isClosed() { 131 return closed; 132 } 133 134 139 public void addListener(IOStreamConnectorListener l) { 140 listenerList.addElement(l); 141 } 142 143 148 public void removeListener(IOStreamConnectorListener l) { 149 listenerList.removeElement(l); 150 } 151 152 class IOStreamConnectorThread 153 implements Runnable { 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 read = in.read(buffer, 0, buffer.length); 164 165 if (read > 0) { 166 167 out.write(buffer, 0, read); 169 170 bytes += read; 172 173 out.flush(); 175 176 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 ioe) { 189 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 ex) {} 203 } 204 205 if (closeOutput) { 206 try { 207 out.close(); 208 } 209 catch (IOException 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
| Popular Tags
|