X-Git-Url: https://git.gag.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fnet%2Fsf%2Fopenrocket%2Futil%2FSaveFileWorker.java;h=9f94d5105b91a8a4a2c5ef543ad2b4c6306f6ebb;hb=6afc62224f6f7e581b1d321e125ed97a6ec77dc1;hp=1a4ab534033b6f407180f987884b98385d31a9e7;hpb=6930dda4b30229cfc3b34f51561e323fd4f9936b;p=debian%2Fopenrocket diff --git a/src/net/sf/openrocket/util/SaveFileWorker.java b/src/net/sf/openrocket/util/SaveFileWorker.java index 1a4ab534..9f94d510 100644 --- a/src/net/sf/openrocket/util/SaveFileWorker.java +++ b/src/net/sf/openrocket/util/SaveFileWorker.java @@ -3,10 +3,6 @@ package net.sf.openrocket.util; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.io.OutputStream; import javax.swing.SwingWorker; @@ -28,9 +24,21 @@ public class SaveFileWorker extends SwingWorker { @Override protected Void doInBackground() throws Exception { + + int estimate = (int)saver.estimateFileSize(document, + document.getDefaultStorageOptions()); + + // Create the ProgressOutputStream that provides progress estimates ProgressOutputStream os = new ProgressOutputStream( new BufferedOutputStream(new FileOutputStream(file)), - (int)saver.estimateFileSize(document, document.getDefaultStorageOptions())); + estimate, this) { + + @Override + protected void setProgress(int progress) { + SaveFileWorker.this.setProgress(progress); + } + + }; try { saver.save(os, document); @@ -45,55 +53,4 @@ public class SaveFileWorker extends SwingWorker { return null; } - - private class ProgressOutputStream extends FilterOutputStream { - - private final int totalBytes; - private int writtenBytes = 0; - private int progress = -1; - - public ProgressOutputStream(OutputStream out, int estimate) { - super(out); - this.totalBytes = estimate; - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - out.write(b, off, len); - writtenBytes += len; - setProgress(); - if (isCancelled()) { - throw new InterruptedIOException("SaveFileWorker was cancelled"); - } - } - - @Override - public void write(byte[] b) throws IOException { - out.write(b); - writtenBytes += b.length; - setProgress(); - if (isCancelled()) { - throw new InterruptedIOException("SaveFileWorker was cancelled"); - } - } - - @Override - public void write(int b) throws IOException { - out.write(b); - writtenBytes++; - setProgress(); - if (isCancelled()) { - throw new InterruptedIOException("SaveFileWorker was cancelled"); - } - } - - - private void setProgress() { - int p = MathUtil.clamp(writtenBytes * 100 / totalBytes, 0, 100); - if (progress != p) { - progress = p; - SaveFileWorker.this.setProgress(progress); - } - } - } }