1 2 24 25 26 27 28 29 package com.lutris.util; 30 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.util.Date ; 34 import java.util.Vector ; 35 36 37 74 public class OutputStreamEventQueue extends OutputStream { 75 76 private boolean open; 78 79 private Vector queue; 81 82 private int numBytes; 84 85 private int maxBytes; 87 88 92 public OutputStreamEventQueue() { 93 this(0); 94 } 95 96 100 public OutputStreamEventQueue(int maxBytes) { 101 this.maxBytes = maxBytes; 102 this.numBytes = 0; 103 open = true; 104 queue = new Vector (); 105 } 106 107 113 114 121 public void write(int b) throws IOException { 122 synchronized (queue) { 123 if (!open) 124 throw new IOException ("Wrote to closed OutputStreamEventQueue."); 125 OutputStreamEventQueueEntry evt = new OutputStreamEventQueueEntry(); 126 evt.when = new Date (); 127 evt.data = new byte[1]; 128 evt.data[0] = (byte) b; 129 queue.addElement(evt); 130 this.numBytes += 4; 131 limitQueueSize(); 132 queue.notify(); } 134 } 135 136 137 144 public void write(byte b[]) throws IOException { 145 synchronized (queue) { 146 if (!open) 147 throw new IOException ("Wrote to closed OutputStreamEventQueue."); 148 OutputStreamEventQueueEntry evt = new OutputStreamEventQueueEntry(new Date (), b); 149 queue.addElement(evt); 150 this.numBytes += b.length; 151 limitQueueSize(); 152 queue.notify(); } 154 } 155 156 157 165 public void write(byte b[], 166 int off, 167 int len) throws IOException { 168 synchronized (queue) { 169 if (!open) 170 throw new IOException ("Wrote to closed OutputStreamEventQueue."); 171 OutputStreamEventQueueEntry evt = new OutputStreamEventQueueEntry(); 172 evt.when = new Date (); 173 byte[] c = new byte[len]; 174 for (int i=0; i<len; i++) 175 c[i] = b[i + off]; 176 evt.data = c; 177 queue.addElement(evt); 178 this.numBytes += b.length; 179 limitQueueSize(); 180 queue.notify(); } 182 } 183 184 185 193 public void flush() throws IOException { 194 synchronized (queue) { 195 if (!open) 196 throw new IOException ("Flushed a closed OutputStreamEventQueue."); 197 } 198 } 199 200 201 212 public void close() throws IOException { 213 synchronized (queue) { 214 if (!open) 215 throw new IOException ("Closed an already closed OutputStreamEventQueue."); 216 open = false; 217 queue.notifyAll(); } 220 } 221 222 223 224 231 private void limitQueueSize() { 232 if (maxBytes == 0) 233 return; 234 while (numBytes > maxBytes) { 235 if (queue.size() <= 0) { 236 numBytes = 0; 237 return; 238 } 239 OutputStreamEventQueueEntry evt = 240 (OutputStreamEventQueueEntry) queue.firstElement(); 241 queue.removeElement(evt); 242 numBytes -= evt.data.length; 243 if (numBytes < 0 ) 244 numBytes = 0; 245 } 246 } 247 248 249 255 267 public OutputStreamEventQueueEntry getEvent() { 268 while (true) { 269 synchronized (queue) { 271 if (queue.size() > 0) { 273 OutputStreamEventQueueEntry evt = 275 (OutputStreamEventQueueEntry) queue.firstElement(); 276 queue.removeElement(evt); 277 numBytes -= evt.data.length; 278 if (numBytes < 0) 279 numBytes = 0; 280 return evt; 281 } else { 282 if (!open) 284 return null; 285 } 286 try { 288 queue.wait(); 289 } catch (InterruptedException e) { 290 } 291 } 292 } 293 } 294 295 296 303 public boolean hasEventsPending() { 304 synchronized (queue) { 305 return (queue.size() > 0); 306 } 307 } 308 } 309 310 311 | Popular Tags |