source: josm/trunk/src/org/openstreetmap/josm/io/ProgressOutputStream.java@ 13790

Last change on this file since 13790 was 12711, checked in by Don-vip, 7 years ago

see #15141, see #15167 - use correct message ("Downloading data" instead of "Uploading data") when downloading data from Overpass API / POST

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.OutputStream;
8
9import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
10import org.openstreetmap.josm.gui.progress.ProgressMonitor;
11
12/**
13 * An {@link OutputStream} which reports progress to the {@link ProgressMonitor}.
14 *
15 * @since 9185
16 */
17public 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}
Note: See TracBrowser for help on using the repository browser.