1 7 8 9 10 package javax.swing; 11 12 13 14 import java.io.*; 15 import java.awt.Component ; 16 17 18 19 47 public class ProgressMonitorInputStream extends FilterInputStream 48 { 49 private ProgressMonitor monitor; 50 private int nread = 0; 51 private int size = 0; 52 53 54 63 public ProgressMonitorInputStream(Component parentComponent, 64 Object message, 65 InputStream in) { 66 super(in); 67 try { 68 size = in.available(); 69 } 70 catch(IOException ioe) { 71 size = 0; 72 } 73 monitor = new ProgressMonitor (parentComponent, message, null, 0, size); 74 } 75 76 77 83 public ProgressMonitor getProgressMonitor() { 84 return monitor; 85 } 86 87 88 92 public int read() throws IOException { 93 int c = in.read(); 94 if (c >= 0) monitor.setProgress(++nread); 95 if (monitor.isCanceled()) { 96 InterruptedIOException exc = 97 new InterruptedIOException("progress"); 98 exc.bytesTransferred = nread; 99 throw exc; 100 } 101 return c; 102 } 103 104 105 109 public int read(byte b[]) throws IOException { 110 int nr = in.read(b); 111 if (nr > 0) monitor.setProgress(nread += nr); 112 if (monitor.isCanceled()) { 113 InterruptedIOException exc = 114 new InterruptedIOException("progress"); 115 exc.bytesTransferred = nread; 116 throw exc; 117 } 118 return nr; 119 } 120 121 122 126 public int read(byte b[], 127 int off, 128 int len) throws IOException { 129 int nr = in.read(b, off, len); 130 if (nr > 0) monitor.setProgress(nread += nr); 131 if (monitor.isCanceled()) { 132 InterruptedIOException exc = 133 new InterruptedIOException("progress"); 134 exc.bytesTransferred = nread; 135 throw exc; 136 } 137 return nr; 138 } 139 140 141 145 public long skip(long n) throws IOException { 146 long nr = in.skip(n); 147 if (nr > 0) monitor.setProgress(nread += nr); 148 return nr; 149 } 150 151 152 156 public void close() throws IOException { 157 in.close(); 158 monitor.close(); 159 } 160 161 162 166 public synchronized void reset() throws IOException { 167 in.reset(); 168 nread = size - in.available(); 169 monitor.setProgress(nread); 170 } 171 } 172 | Popular Tags |