1 21 24 package org.lobobrowser.util; 25 26 import java.io.*; 27 28 31 public class MonitoredInputStream extends InputStream { 32 private final InputStream delegate; 33 private int progress = 0; 34 private final long minProgressEventGap; 35 public final EventDispatch evtProgress = new EventDispatch(); 36 37 public MonitoredInputStream(InputStream delegate, int minProgressEventGap) { 38 this.delegate = delegate; 39 this.minProgressEventGap = minProgressEventGap; 40 } 41 42 public MonitoredInputStream(InputStream delegate) { 43 this(delegate, 200); 44 } 45 46 49 public int available() throws IOException { 50 return this.delegate.available(); 51 } 52 53 56 public void close() throws IOException { 57 this.delegate.close(); 58 } 59 60 63 public boolean markSupported() { 64 return false; 65 } 66 67 70 public int read() throws IOException { 71 int b = this.delegate.read(); 72 if(b != -1) { 73 this.progress++; 74 } 75 return b; 76 } 77 78 private long lastEvenPosted = 0; 79 80 83 public int read(byte[] arg0, int arg1, int arg2) throws IOException { 84 int numRead = this.delegate.read(arg0, arg1, arg2); 85 if(numRead != -1) { 86 this.progress += numRead; 87 long currentTime = System.currentTimeMillis(); 88 if(currentTime - this.lastEvenPosted > this.minProgressEventGap) { 89 this.evtProgress.fireEvent(new InputProgressEvent(this, this.progress)); 90 this.lastEvenPosted = currentTime; 91 } 92 } 93 return numRead; 94 } 95 } 96 97 | Popular Tags |