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