1 | // License: GPL. For details, see LICENSE file.
|
---|
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;
|
---|
21 | private final boolean finishOnClose;
|
---|
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
|
---|
29 | * @param finishOnClose whether to call {@link ProgressMonitor#finishTask} when this stream is closed
|
---|
30 | * @since 10302
|
---|
31 | */
|
---|
32 | public ProgressOutputStream(OutputStream out, long size, ProgressMonitor progressMonitor, boolean finishOnClose) {
|
---|
33 | this(out, size, progressMonitor, tr("Uploading data ..."), finishOnClose);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code ProgressOutputStream}.
|
---|
38 | *
|
---|
39 | * @param out the stream to monitor
|
---|
40 | * @param size the total size which will be sent
|
---|
41 | * @param progressMonitor the monitor to report to
|
---|
42 | * @param message the message that will be displayed by the inner {@link StreamProgressUpdater}
|
---|
43 | * @param finishOnClose whether to call {@link ProgressMonitor#finishTask} when this stream is closed
|
---|
44 | * @since 12711
|
---|
45 | */
|
---|
46 | public ProgressOutputStream(OutputStream out, long size, ProgressMonitor progressMonitor, String message, boolean finishOnClose) {
|
---|
47 | this.updater = new StreamProgressUpdater(size,
|
---|
48 | progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE, message);
|
---|
49 | this.out = out;
|
---|
50 | this.finishOnClose = finishOnClose;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void write(byte[] b, int off, int len) throws IOException {
|
---|
55 | out.write(b, off, len);
|
---|
56 | updater.advanceTicker(len);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void write(int b) throws IOException {
|
---|
61 | out.write(b);
|
---|
62 | updater.advanceTicker(1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void close() throws IOException {
|
---|
67 | try {
|
---|
68 | out.close();
|
---|
69 | } finally {
|
---|
70 | if (finishOnClose) {
|
---|
71 | updater.finishTask();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|