[9197] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[9185] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.io.OutputStream;
|
---|
| 8 |
|
---|
| 9 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 10 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * An {@link OutputStream} which reports progress to the {@link ProgressMonitor}.
|
---|
| 14 | *
|
---|
| 15 | * @since 9185
|
---|
| 16 | */
|
---|
| 17 | public class ProgressOutputStream extends OutputStream {
|
---|
| 18 |
|
---|
| 19 | private final StreamProgressUpdater updater;
|
---|
| 20 | private final OutputStream out;
|
---|
[10302] | 21 | private final boolean finishOnClose;
|
---|
[9185] | 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Constructs a new {@code ProgressOutputStream}.
|
---|
| 25 | *
|
---|
| 26 | * @param out the stream to monitor
|
---|
| 27 | * @param size the total size which will be sent
|
---|
| 28 | * @param progressMonitor the monitor to report to
|
---|
[10302] | 29 | * @param finishOnClose whether to call {@link ProgressMonitor#finishTask} when this stream is closed
|
---|
| 30 | * @since 10302
|
---|
[9185] | 31 | */
|
---|
[10302] | 32 | public ProgressOutputStream(OutputStream out, long size, ProgressMonitor progressMonitor, boolean finishOnClose) {
|
---|
| 33 | this.updater = new StreamProgressUpdater(size,
|
---|
| 34 | progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE, tr("Uploading data ..."));
|
---|
[9185] | 35 | this.out = out;
|
---|
[10302] | 36 | this.finishOnClose = finishOnClose;
|
---|
[9185] | 37 | }
|
---|
| 38 |
|
---|
| 39 | @Override
|
---|
| 40 | public void write(byte[] b, int off, int len) throws IOException {
|
---|
| 41 | out.write(b, off, len);
|
---|
| 42 | updater.advanceTicker(len);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public void write(int b) throws IOException {
|
---|
| 47 | out.write(b);
|
---|
| 48 | updater.advanceTicker(1);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public void close() throws IOException {
|
---|
| 53 | try {
|
---|
| 54 | out.close();
|
---|
| 55 | } finally {
|
---|
[10302] | 56 | if (finishOnClose) {
|
---|
| 57 | updater.finishTask();
|
---|
| 58 | }
|
---|
[9185] | 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|