X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fnet%2Fsf%2Fopenrocket%2Futil%2FSaveFileWorker.java;h=fe5f70cfb51f23c4992b18d71d5278436e98af48;hb=e417c9e951ae308b2fe5d34dd69f0a5e7651d99a;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..fe5f70cf 100644 --- a/src/net/sf/openrocket/util/SaveFileWorker.java +++ b/src/net/sf/openrocket/util/SaveFileWorker.java @@ -3,15 +3,12 @@ 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; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.file.RocketSaver; +import net.sf.openrocket.gui.main.ExceptionHandler; public class SaveFileWorker extends SwingWorker { @@ -28,9 +25,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); @@ -38,62 +47,10 @@ public class SaveFileWorker extends SwingWorker { try { os.close(); } catch (Exception e) { - System.err.println("Error closing file: "); - e.printStackTrace(); + ExceptionHandler.handleErrorCondition("Error closing file", e); } } 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); - } - } - } }