2 * TODO: CRITICAL: Licensing
5 package net.sf.openrocket.util;
7 import java.awt.Component;
8 import java.io.FilterInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InterruptedIOException;
15 * A functional equivalent of <code>ProgressMonitorInputStream</code> which
16 * uses {@link ConcurrentProgressMonitor} and leaves the progress dialog open
17 * to be manually closed later on.
20 public class ConcurrentProgressMonitorInputStream extends FilterInputStream {
21 private ConcurrentProgressMonitor monitor;
22 private int nread = 0;
27 * Constructs an object to monitor the progress of an input stream.
29 * @param message Descriptive text to be placed in the dialog box
30 * if one is popped up.
31 * @param parentComponent The component triggering the operation
33 * @param in The input stream to be monitored.
35 public ConcurrentProgressMonitorInputStream(Component parentComponent,
36 Object message, InputStream in) {
39 size = in.available();
40 } catch (IOException ioe) {
43 monitor = new ConcurrentProgressMonitor(parentComponent, message, null, 0,
49 * Get the ProgressMonitor object being used by this stream. Normally
50 * this isn't needed unless you want to do something like change the
51 * descriptive text partway through reading the file.
52 * @return the ProgressMonitor object used by this object
54 public ConcurrentProgressMonitor getProgressMonitor() {
60 * Overrides <code>FilterInputStream.read</code>
61 * to update the progress monitor after the read.
64 public int read() throws IOException {
67 monitor.setProgress(++nread);
68 if (monitor.isCanceled()) {
69 InterruptedIOException exc = new InterruptedIOException("progress");
70 exc.bytesTransferred = nread;
78 * Overrides <code>FilterInputStream.read</code>
79 * to update the progress monitor after the read.
82 public int read(byte b[]) throws IOException {
85 monitor.setProgress(nread += nr);
86 if (monitor.isCanceled()) {
87 InterruptedIOException exc = new InterruptedIOException("progress");
88 exc.bytesTransferred = nread;
96 * Overrides <code>FilterInputStream.read</code>
97 * to update the progress monitor after the read.
100 public int read(byte b[], int off, int len) throws IOException {
101 int nr = in.read(b, off, len);
103 monitor.setProgress(nread += nr);
104 if (monitor.isCanceled()) {
105 InterruptedIOException exc = new InterruptedIOException("progress");
106 exc.bytesTransferred = nread;
114 * Overrides <code>FilterInputStream.skip</code>
115 * to update the progress monitor after the skip.
118 public long skip(long n) throws IOException {
119 long nr = in.skip(n);
121 monitor.setProgress(nread += nr);
127 * Overrides <code>FilterInputStream.close</code>
128 * to close the progress monitor as well as the stream.
131 public void close() throws IOException {
137 * Overrides <code>FilterInputStream.reset</code>
138 * to reset the progress monitor as well as the stream.
141 public synchronized void reset() throws IOException {
143 nread = size - in.available();
144 monitor.setProgress(nread);