1 package net.sf.openrocket.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;
11 * A functional equivalent of <code>ProgressMonitorInputStream</code> which
12 * uses {@link ConcurrentProgressMonitor} and leaves the progress dialog open
13 * to be manually closed later on.
16 public class ConcurrentProgressMonitorInputStream extends FilterInputStream {
17 private ConcurrentProgressMonitor monitor;
18 private int nread = 0;
23 * Constructs an object to monitor the progress of an input stream.
25 * @param message Descriptive text to be placed in the dialog box
26 * if one is popped up.
27 * @param parentComponent The component triggering the operation
29 * @param in The input stream to be monitored.
31 public ConcurrentProgressMonitorInputStream(Component parentComponent,
32 Object message, InputStream in) {
35 size = in.available();
36 } catch (IOException ioe) {
39 monitor = new ConcurrentProgressMonitor(parentComponent, message, null, 0,
45 * Get the ProgressMonitor object being used by this stream. Normally
46 * this isn't needed unless you want to do something like change the
47 * descriptive text partway through reading the file.
48 * @return the ProgressMonitor object used by this object
50 public ConcurrentProgressMonitor getProgressMonitor() {
56 * Overrides <code>FilterInputStream.read</code>
57 * to update the progress monitor after the read.
60 public int read() throws IOException {
63 monitor.setProgress(++nread);
64 if (monitor.isCanceled()) {
65 InterruptedIOException exc = new InterruptedIOException("progress");
66 exc.bytesTransferred = nread;
74 * Overrides <code>FilterInputStream.read</code>
75 * to update the progress monitor after the read.
78 public int read(byte b[]) throws IOException {
81 monitor.setProgress(nread += nr);
82 if (monitor.isCanceled()) {
83 InterruptedIOException exc = new InterruptedIOException("progress");
84 exc.bytesTransferred = nread;
92 * Overrides <code>FilterInputStream.read</code>
93 * to update the progress monitor after the read.
96 public int read(byte b[], int off, int len) throws IOException {
97 int nr = in.read(b, off, len);
99 monitor.setProgress(nread += nr);
100 if (monitor.isCanceled()) {
101 InterruptedIOException exc = new InterruptedIOException("progress");
102 exc.bytesTransferred = nread;
110 * Overrides <code>FilterInputStream.skip</code>
111 * to update the progress monitor after the skip.
114 public long skip(long n) throws IOException {
115 long nr = in.skip(n);
117 monitor.setProgress(nread += nr);
123 * Overrides <code>FilterInputStream.close</code>
124 * to close the progress monitor as well as the stream.
127 public void close() throws IOException {
134 * Overrides <code>FilterInputStream.reset</code>
135 * to reset the progress monitor as well as the stream.
138 public synchronized void reset() throws IOException {
140 nread = size - in.available();
141 monitor.setProgress(nread);