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