-
- 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);
- }
- }
- }