1 19 20 package com.maverick.util; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InterruptedIOException ; 25 import java.io.OutputStream ; 26 27 39 public class DynamicBuffer { 40 41 42 protected static final int DEFAULT_BUFFER_SIZE = 32768; 43 44 45 protected byte[] buf; 46 47 48 protected int writepos = 0; 49 50 51 protected int readpos = 0; 52 53 54 protected InputStream in; 55 56 57 protected OutputStream out; 58 private boolean closed = false; 59 private int interrupt = 5000; 60 61 64 public DynamicBuffer() { 65 buf = new byte[DEFAULT_BUFFER_SIZE]; 66 in = new DynamicBufferInputStream(); 67 out = new DynamicBufferOutputStream(); 68 } 69 70 76 public InputStream getInputStream() { 77 return in; 78 } 79 80 86 public OutputStream getOutputStream() { 87 return out; 88 } 89 90 private synchronized void verifyBufferSize(int count) { 91 if (count > (buf.length - writepos)) { 94 System.arraycopy(buf, readpos, buf, 0, writepos - readpos); 95 writepos -= readpos; 96 readpos = 0; 97 } 98 99 if (count > (buf.length - writepos)) { 101 byte[] tmp = new byte[buf.length + DEFAULT_BUFFER_SIZE]; 102 System.arraycopy(buf, 0, tmp, 0, writepos - readpos); 103 buf = tmp; 104 } 105 } 106 107 111 protected synchronized int available() { 112 return writepos - readpos > 0 113 ? writepos - readpos 114 : (closed ? -1 : 0); 115 } 116 117 private synchronized void block() throws InterruptedException { 118 119 if (!closed) { 121 while ( (readpos >= writepos) && !closed) { 122 wait(interrupt); 123 } 124 } 125 } 126 127 130 public synchronized void close() { 131 if (!closed) { 132 closed = true; 133 notifyAll(); 134 } 135 } 136 137 144 protected synchronized void write(int b) throws IOException { 145 if (closed) { 146 throw new IOException ("The buffer is closed"); 147 } 148 149 verifyBufferSize(1); 150 buf[writepos] = (byte) b; 151 writepos++; 152 153 notifyAll(); 154 } 155 156 165 protected synchronized void write(byte[] data, int offset, int len) throws 166 IOException { 167 168 if (closed) { 169 throw new IOException ("The buffer is closed"); 170 } 171 172 verifyBufferSize(len); 173 System.arraycopy(data, offset, buf, writepos, len); 174 writepos += len; 175 176 notifyAll(); 177 } 178 179 public void setBlockInterrupt(int interrupt) { 180 this.interrupt = interrupt; 181 } 182 183 191 protected synchronized int read() throws IOException { 192 try { 193 block(); 194 } 195 catch (InterruptedException ex) { 196 throw new InterruptedIOException ( 197 "The blocking operation was interrupted"); 198 } 199 200 if (closed && available() <= 0) { 201 return -1; 202 } 203 204 return (int) buf[readpos++]; 205 206 } 207 208 220 protected synchronized int read(byte[] data, int offset, int len) throws 221 IOException { 222 try { 223 224 block(); 225 } 226 catch (InterruptedException ex) { 227 throw new InterruptedIOException ( 228 "The blocking operation was interrupted"); 229 } 230 231 if (closed && available() <= 0) { 232 return -1; 233 } 234 235 int read = (len > (writepos - readpos)) ? (writepos - readpos) : len; 236 System.arraycopy(buf, readpos, data, offset, read); 237 readpos += read; 238 239 return read; 240 241 } 242 243 248 protected synchronized void flush() throws IOException { 249 notifyAll(); 250 } 251 252 class DynamicBufferInputStream 253 extends InputStream { 254 public int read() throws IOException { 255 return DynamicBuffer.this.read(); 256 } 257 258 public int read(byte[] data, int offset, int len) throws IOException { 259 return DynamicBuffer.this.read(data, offset, len); 260 } 261 262 public int available() { 263 return DynamicBuffer.this.available(); 264 } 265 266 public void close() { 267 DynamicBuffer.this.close(); 268 } 269 } 270 271 class DynamicBufferOutputStream 272 extends OutputStream { 273 public void write(int b) throws IOException { 274 DynamicBuffer.this.write(b); 275 } 276 277 public void write(byte[] data, int offset, int len) throws IOException { 278 DynamicBuffer.this.write(data, offset, len); 279 } 280 281 public void flush() throws IOException { 282 DynamicBuffer.this.flush(); 283 } 284 285 public void close() { 286 DynamicBuffer.this.close(); 287 } 288 } 289 } 290 | Popular Tags |