1 package net.sf.openrocket.util;
3 import java.io.FilterOutputStream;
4 import java.io.IOException;
5 import java.io.InterruptedIOException;
6 import java.io.OutputStream;
8 import javax.swing.SwingWorker;
11 public abstract class ProgressOutputStream extends FilterOutputStream {
13 private final int totalBytes;
14 private final SwingWorker<?,?> worker;
15 private int writtenBytes = 0;
16 private int progress = -1;
18 public ProgressOutputStream(OutputStream out, int estimate, SwingWorker<?,?> worker) {
20 this.totalBytes = estimate;
25 public void write(byte[] b, int off, int len) throws IOException {
26 out.write(b, off, len);
29 if (worker.isCancelled()) {
30 throw new InterruptedIOException("SaveFileWorker was cancelled");
35 public void write(byte[] b) throws IOException {
37 writtenBytes += b.length;
39 if (worker.isCancelled()) {
40 throw new InterruptedIOException("SaveFileWorker was cancelled");
45 public void write(int b) throws IOException {
49 if (worker.isCancelled()) {
50 throw new InterruptedIOException("SaveFileWorker was cancelled");
55 private void setProgress() {
56 int p = MathUtil.clamp(writtenBytes * 100 / totalBytes, 0, 100);
59 setProgress(progress);
64 * Set the current progress. The value of <code>progress</code> is guaranteed
65 * to be between 0 and 100, inclusive.
67 * @param progress the current progress in the range 0-100.
69 protected abstract void setProgress(int progress);