KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ProgressMonitorInputStream


1 /*
2  * @(#)ProgressMonitorInputStream.java 1.19 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9
10 package javax.swing;
11
12
13
14 import java.io.*;
15 import java.awt.Component JavaDoc;
16
17
18
19 /**
20  * Monitors the progress of reading from some InputStream. This ProgressMonitor
21  * is normally invoked in roughly this form:
22  * <pre>
23  * InputStream in = new BufferedInputStream(
24  * new ProgressMonitorInputStream(
25  * parentComponent,
26  * "Reading " + fileName,
27  * new FileInputStream(fileName)));
28  * </pre><p>
29  * This creates a progress monitor to monitor the progress of reading
30  * the input stream. If it's taking a while, a ProgressDialog will
31  * be popped up to inform the user. If the user hits the Cancel button
32  * an InterruptedIOException will be thrown on the next read.
33  * All the right cleanup is done when the stream is closed.
34  *
35  *
36  * <p>
37  *
38  * For further documentation and examples see
39  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html">How to Monitor Progress</a>,
40  * a section in <em>The Java Tutorial.</em>
41  *
42  * @see ProgressMonitor
43  * @see JOptionPane
44  * @author James Gosling
45  * @version 1.19 12/19/03
46  */

47 public class ProgressMonitorInputStream extends FilterInputStream
48 {
49     private ProgressMonitor JavaDoc monitor;
50     private int nread = 0;
51     private int size = 0;
52
53
54     /**
55      * Constructs an object to monitor the progress of an input stream.
56      *
57      * @param message Descriptive text to be placed in the dialog box
58      * if one is popped up.
59      * @param parentComponent The component triggering the operation
60      * being monitored.
61      * @param in The input stream to be monitored.
62      */

63     public ProgressMonitorInputStream(Component JavaDoc parentComponent,
64                                       Object JavaDoc message,
65                                       InputStream in) {
66         super(in);
67         try {
68             size = in.available();
69         }
70         catch(IOException ioe) {
71             size = 0;
72         }
73         monitor = new ProgressMonitor JavaDoc(parentComponent, message, null, 0, size);
74     }
75
76
77     /**
78      * Get the ProgressMonitor object being used by this stream. Normally
79      * this isn't needed unless you want to do something like change the
80      * descriptive text partway through reading the file.
81      * @return the ProgressMonitor object used by this object
82      */

83     public ProgressMonitor JavaDoc getProgressMonitor() {
84         return monitor;
85     }
86
87
88     /**
89      * Overrides <code>FilterInputStream.read</code>
90      * to update the progress monitor after the read.
91      */

92     public int read() throws IOException {
93         int c = in.read();
94         if (c >= 0) monitor.setProgress(++nread);
95         if (monitor.isCanceled()) {
96             InterruptedIOException exc =
97                                     new InterruptedIOException("progress");
98             exc.bytesTransferred = nread;
99             throw exc;
100         }
101         return c;
102     }
103
104
105     /**
106      * Overrides <code>FilterInputStream.read</code>
107      * to update the progress monitor after the read.
108      */

109     public int read(byte b[]) throws IOException {
110         int nr = in.read(b);
111         if (nr > 0) monitor.setProgress(nread += nr);
112         if (monitor.isCanceled()) {
113             InterruptedIOException exc =
114                                     new InterruptedIOException("progress");
115             exc.bytesTransferred = nread;
116             throw exc;
117         }
118         return nr;
119     }
120
121
122     /**
123      * Overrides <code>FilterInputStream.read</code>
124      * to update the progress monitor after the read.
125      */

126     public int read(byte b[],
127                     int off,
128                     int len) throws IOException {
129         int nr = in.read(b, off, len);
130         if (nr > 0) monitor.setProgress(nread += nr);
131         if (monitor.isCanceled()) {
132             InterruptedIOException exc =
133                                     new InterruptedIOException("progress");
134             exc.bytesTransferred = nread;
135             throw exc;
136         }
137         return nr;
138     }
139
140
141     /**
142      * Overrides <code>FilterInputStream.skip</code>
143      * to update the progress monitor after the skip.
144      */

145     public long skip(long n) throws IOException {
146         long nr = in.skip(n);
147         if (nr > 0) monitor.setProgress(nread += nr);
148         return nr;
149     }
150
151
152     /**
153      * Overrides <code>FilterInputStream.close</code>
154      * to close the progress monitor as well as the stream.
155      */

156     public void close() throws IOException {
157         in.close();
158         monitor.close();
159     }
160
161
162     /**
163      * Overrides <code>FilterInputStream.reset</code>
164      * to reset the progress monitor as well as the stream.
165      */

166     public synchronized void reset() throws IOException {
167         in.reset();
168         nread = size - in.available();
169         monitor.setProgress(nread);
170     }
171 }
172
Popular Tags