KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > MonitoredInputStream


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Apr 15, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.io.*;
27
28 /**
29  * @author J. H. S.
30  */

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     /* (non-Javadoc)
47      * @see java.io.InputStream#available()
48      */

49     public int available() throws IOException {
50         return this.delegate.available();
51     }
52
53     /* (non-Javadoc)
54      * @see java.io.InputStream#close()
55      */

56     public void close() throws IOException {
57         this.delegate.close();
58     }
59
60     /* (non-Javadoc)
61      * @see java.io.InputStream#markSupported()
62      */

63     public boolean markSupported() {
64         return false;
65     }
66     
67     /* (non-Javadoc)
68      * @see java.io.InputStream#read()
69      */

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     /* (non-Javadoc)
81      * @see java.io.InputStream#read(byte[], int, int)
82      */

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