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

Last change on this file since 12667 was 10302, checked in by Don-vip, 8 years ago

fix #12583 - fix unit tests by adding a new mode to HttpClient. Don't know if it's the best way to do it...

  • Property svn:eol-style set to native
File size: 1.8 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.updater = new StreamProgressUpdater(size,
34 progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE, tr("Uploading data ..."));
35 this.out = out;
36 this.finishOnClose = finishOnClose;
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 {
56 if (finishOnClose) {
57 updater.finishTask();
58 }
59 }
60 }
61}
Note: See TracBrowser for help on using the repository browser.