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 |
|
---|
22 | /**
|
---|
23 | * Constructs a new {@code ProgressOutputStream}.
|
---|
24 | *
|
---|
25 | * @param out the stream to monitor
|
---|
26 | * @param size the total size which will be sent
|
---|
27 | * @param progressMonitor the monitor to report to
|
---|
28 | */
|
---|
29 | public ProgressOutputStream(OutputStream out, long size, ProgressMonitor progressMonitor) {
|
---|
30 | if (progressMonitor == null) {
|
---|
31 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
32 | }
|
---|
33 | this.updater = new StreamProgressUpdater(size, progressMonitor, tr("Uploading data ..."));
|
---|
34 | this.out = out;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void write(byte[] b, int off, int len) throws IOException {
|
---|
39 | out.write(b, off, len);
|
---|
40 | updater.advanceTicker(len);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void write(int b) throws IOException {
|
---|
45 | out.write(b);
|
---|
46 | updater.advanceTicker(1);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void close() throws IOException {
|
---|
51 | try {
|
---|
52 | out.close();
|
---|
53 | } finally {
|
---|
54 | updater.finishTask();
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|