source: josm/trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java@ 9197

Last change on this file since 9197 was 9185, checked in by simon04, 8 years ago

see #12231 - HttpClient now reports status to ProgressMonitor

  • Property svn:eol-style set to native
File size: 2.9 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.InputStream;
8import java.net.URLConnection;
9
10import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
11import org.openstreetmap.josm.gui.progress.ProgressMonitor;
12
13/**
14 * Read from an other reader and increment an progress counter while on the way.
15 * @author Imi
16 */
17public class ProgressInputStream extends InputStream {
18
19 private final StreamProgressUpdater updater;
20 private final InputStream in;
21
22 /**
23 * Constructs a new {@code ProgressInputStream}.
24 *
25 * @param in the stream to monitor
26 * @param size the total size which will be sent
27 * @param progressMonitor the monitor to report to
28 * @since 9172
29 */
30 public ProgressInputStream(InputStream in, long size, ProgressMonitor progressMonitor) {
31 if (progressMonitor == null) {
32 progressMonitor = NullProgressMonitor.INSTANCE;
33 }
34 this.updater = new StreamProgressUpdater(size, progressMonitor, tr("Downloading data..."));
35 this.in = in;
36 }
37
38 /**
39 * Constructs a new {@code ProgressInputStream}.
40 *
41 * Will call {@link URLConnection#getInputStream()} to obtain the stream to monitor.
42 *
43 * @param con the connection to monitor
44 * @param progressMonitor the monitor to report to
45 */
46 public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
47 if (progressMonitor == null) {
48 progressMonitor = NullProgressMonitor.INSTANCE;
49 }
50 progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
51 progressMonitor.indeterminateSubTask(null);
52
53 try {
54 this.in = con.getInputStream();
55 this.updater = new StreamProgressUpdater(con.getContentLength(), progressMonitor, tr("Downloading data..."));
56 } catch (IOException e) {
57 progressMonitor.finishTask();
58 if (con.getHeaderField("Error") != null)
59 throw new OsmTransferException(tr(con.getHeaderField("Error")), e);
60 throw new OsmTransferException(e);
61 }
62 }
63
64 @Override
65 public void close() throws IOException {
66 try {
67 in.close();
68 } finally {
69 updater.finishTask();
70 }
71 }
72
73 @Override
74 public int read(byte[] b, int off, int len) throws IOException {
75 int read = in.read(b, off, len);
76 if (read != -1) {
77 updater.advanceTicker(read);
78 } else {
79 updater.finishTask();
80 }
81 return read;
82 }
83
84 @Override
85 public int read() throws IOException {
86 int read = in.read();
87 if (read != -1) {
88 updater.advanceTicker(1);
89 } else {
90 updater.finishTask();
91 }
92 return read;
93 }
94}
Note: See TracBrowser for help on using the repository browser.