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

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

refactor handling of null values - use Java 8 Optional where possible

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