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

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

OAuth: add robustness, basic unit test, code cleanup

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