updates for 0.9.2
[debian/openrocket] / src / net / sf / openrocket / util / ConcurrentProgressMonitorInputStream.java
1 package net.sf.openrocket.util;
2
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;
8
9
10 /**
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.
14  */
15
16 public class ConcurrentProgressMonitorInputStream extends FilterInputStream {
17         private ConcurrentProgressMonitor monitor;
18         private int nread = 0;
19         private int size = 0;
20
21
22         /**
23          * Constructs an object to monitor the progress of an input stream.
24          *
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
28          *                        being monitored.
29          * @param in The input stream to be monitored.
30          */
31         public ConcurrentProgressMonitorInputStream(Component parentComponent,
32                         Object message, InputStream in) {
33                 super(in);
34                 try {
35                         size = in.available();
36                 } catch (IOException ioe) {
37                         size = 0;
38                 }
39                 monitor = new ConcurrentProgressMonitor(parentComponent, message, null, 0,
40                                 size + 1);
41         }
42
43
44         /**
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 
49          */
50         public ConcurrentProgressMonitor getProgressMonitor() {
51                 return monitor;
52         }
53
54
55         /**
56          * Overrides <code>FilterInputStream.read</code> 
57          * to update the progress monitor after the read.
58          */
59         @Override
60         public int read() throws IOException {
61                 int c = in.read();
62                 if (c >= 0)
63                         monitor.setProgress(++nread);
64                 if (monitor.isCanceled()) {
65                         InterruptedIOException exc = new InterruptedIOException("progress");
66                         exc.bytesTransferred = nread;
67                         throw exc;
68                 }
69                 return c;
70         }
71
72
73         /**
74          * Overrides <code>FilterInputStream.read</code> 
75          * to update the progress monitor after the read.
76          */
77         @Override
78         public int read(byte b[]) throws IOException {
79                 int nr = in.read(b);
80                 if (nr > 0)
81                         monitor.setProgress(nread += nr);
82                 if (monitor.isCanceled()) {
83                         InterruptedIOException exc = new InterruptedIOException("progress");
84                         exc.bytesTransferred = nread;
85                         throw exc;
86                 }
87                 return nr;
88         }
89
90
91         /**
92          * Overrides <code>FilterInputStream.read</code> 
93          * to update the progress monitor after the read.
94          */
95         @Override
96         public int read(byte b[], int off, int len) throws IOException {
97                 int nr = in.read(b, off, len);
98                 if (nr > 0)
99                         monitor.setProgress(nread += nr);
100                 if (monitor.isCanceled()) {
101                         InterruptedIOException exc = new InterruptedIOException("progress");
102                         exc.bytesTransferred = nread;
103                         throw exc;
104                 }
105                 return nr;
106         }
107
108
109         /**
110          * Overrides <code>FilterInputStream.skip</code> 
111          * to update the progress monitor after the skip.
112          */
113         @Override
114         public long skip(long n) throws IOException {
115                 long nr = in.skip(n);
116                 if (nr > 0)
117                         monitor.setProgress(nread += nr);
118                 return nr;
119         }
120
121
122         /**
123          * Overrides <code>FilterInputStream.close</code> 
124          * to close the progress monitor as well as the stream.
125          */
126         @Override
127         public void close() throws IOException {
128                 in.close();
129         monitor.close();
130         }
131
132
133         /**
134          * Overrides <code>FilterInputStream.reset</code> 
135          * to reset the progress monitor as well as the stream.
136          */
137         @Override
138         public synchronized void reset() throws IOException {
139                 in.reset();
140                 nread = size - in.available();
141                 monitor.setProgress(nread);
142         }
143 }