1 package net.sf.openrocket.gui.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;
10 import net.sf.openrocket.util.MathUtil;
13 public abstract class ProgressOutputStream extends FilterOutputStream {
15 private final int totalBytes;
16 private final SwingWorker<?,?> worker;
17 private int writtenBytes = 0;
18 private int progress = -1;
20 public ProgressOutputStream(OutputStream out, int estimate, SwingWorker<?,?> worker) {
22 this.totalBytes = estimate;
27 public void write(byte[] b, int off, int len) throws IOException {
28 out.write(b, off, len);
31 if (worker.isCancelled()) {
32 throw new InterruptedIOException("SaveFileWorker was cancelled");
37 public void write(byte[] b) throws IOException {
39 writtenBytes += b.length;
41 if (worker.isCancelled()) {
42 throw new InterruptedIOException("SaveFileWorker was cancelled");
47 public void write(int b) throws IOException {
51 if (worker.isCancelled()) {
52 throw new InterruptedIOException("SaveFileWorker was cancelled");
57 private void setProgress() {
58 int p = MathUtil.clamp(writtenBytes * 100 / totalBytes, 0, 100);
61 setProgress(progress);
66 * Set the current progress. The value of <code>progress</code> is guaranteed
67 * to be between 0 and 100, inclusive.
69 * @param progress the current progress in the range 0-100.
71 protected abstract void setProgress(int progress);