1 package net.sf.openrocket.gui.util;
3 import java.awt.Component;
4 import java.io.FilterInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InterruptedIOException;
12 * A functional equivalent of <code>ProgressMonitorInputStream</code> which
13 * uses {@link ConcurrentProgressMonitor} and leaves the progress dialog open
14 * to be manually closed later on.
17 public class ConcurrentProgressMonitorInputStream extends FilterInputStream {
18 private ConcurrentProgressMonitor monitor;
19 private int nread = 0;
24 * Constructs an object to monitor the progress of an input stream.
26 * @param message Descriptive text to be placed in the dialog box
27 * if one is popped up.
28 * @param parentComponent The component triggering the operation
30 * @param in The input stream to be monitored.
32 public ConcurrentProgressMonitorInputStream(Component parentComponent,
33 Object message, InputStream in) {
36 size = in.available();
37 } catch (IOException ioe) {
40 monitor = new ConcurrentProgressMonitor(parentComponent, message, null, 0,
46 * Get the ProgressMonitor object being used by this stream. Normally
47 * this isn't needed unless you want to do something like change the
48 * descriptive text partway through reading the file.
49 * @return the ProgressMonitor object used by this object
51 public ConcurrentProgressMonitor getProgressMonitor() {
57 * Overrides <code>FilterInputStream.read</code>
58 * to update the progress monitor after the read.
61 public int read() throws IOException {
64 monitor.setProgress(++nread);
65 if (monitor.isCanceled()) {
66 InterruptedIOException exc = new InterruptedIOException("progress");
67 exc.bytesTransferred = nread;
75 * Overrides <code>FilterInputStream.read</code>
76 * to update the progress monitor after the read.
79 public int read(byte b[]) throws IOException {
82 monitor.setProgress(nread += nr);
83 if (monitor.isCanceled()) {
84 InterruptedIOException exc = new InterruptedIOException("progress");
85 exc.bytesTransferred = nread;
93 * Overrides <code>FilterInputStream.read</code>
94 * to update the progress monitor after the read.
97 public int read(byte b[], int off, int len) throws IOException {
98 int nr = in.read(b, off, len);
100 monitor.setProgress(nread += nr);
101 if (monitor.isCanceled()) {
102 InterruptedIOException exc = new InterruptedIOException("progress");
103 exc.bytesTransferred = nread;
111 * Overrides <code>FilterInputStream.skip</code>
112 * to update the progress monitor after the skip.
115 public long skip(long n) throws IOException {
116 long nr = in.skip(n);
118 monitor.setProgress(nread += nr);
124 * Overrides <code>FilterInputStream.close</code>
125 * to close the progress monitor as well as the stream.
128 public void close() throws IOException {
135 * Overrides <code>FilterInputStream.reset</code>
136 * to reset the progress monitor as well as the stream.
139 public synchronized void reset() throws IOException {
141 nread = size - in.available();
142 monitor.setProgress(nread);