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

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

deprecate ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) - plugins must use HttpClient instead

  • 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;
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 * @deprecated use {@link org.openstreetmap.josm.tools.HttpClient.Response#getContent}
48 */
49 @Deprecated
50 public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
51 if (progressMonitor == null) {
52 progressMonitor = NullProgressMonitor.INSTANCE;
53 }
54 progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
55 progressMonitor.indeterminateSubTask(null);
56
57 try {
58 this.in = con.getInputStream();
59 this.updater = new StreamProgressUpdater(con.getContentLength(), progressMonitor, tr("Downloading data..."));
60 } catch (IOException e) {
61 progressMonitor.finishTask();
62 if (con.getHeaderField("Error") != null)
63 throw new OsmTransferException(tr(con.getHeaderField("Error")), e);
64 throw new OsmTransferException(e);
65 }
66 }
67
68 @Override
69 public void close() throws IOException {
70 try {
71 in.close();
72 } finally {
73 updater.finishTask();
74 }
75 }
76
77 @Override
78 public int read(byte[] b, int off, int len) throws IOException {
79 int read = in.read(b, off, len);
80 if (read != -1) {
81 updater.advanceTicker(read);
82 } else {
83 updater.finishTask();
84 }
85 return read;
86 }
87
88 @Override
89 public int read() throws IOException {
90 int read = in.read();
91 if (read != -1) {
92 updater.advanceTicker(1);
93 } else {
94 updater.finishTask();
95 }
96 return read;
97 }
98}
Note: See TracBrowser for help on using the repository browser.