1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.net.URLConnection;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
11 | import 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 | */
|
---|
17 | public class ProgressInputStream extends InputStream {
|
---|
18 |
|
---|
19 | private final InputStream in;
|
---|
20 | private int readSoFar = 0;
|
---|
21 | private int lastDialogUpdate = 0;
|
---|
22 | private boolean sizeKnown;
|
---|
23 | private final URLConnection connection;
|
---|
24 | private final ProgressMonitor progressMonitor;
|
---|
25 |
|
---|
26 | public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
27 | this.connection = con;
|
---|
28 | if (progressMonitor == null) {
|
---|
29 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
30 | }
|
---|
31 | this.progressMonitor = progressMonitor;
|
---|
32 | progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
|
---|
33 | progressMonitor.indeterminateSubTask(null);
|
---|
34 |
|
---|
35 | try {
|
---|
36 | this.in = con.getInputStream();
|
---|
37 | } catch (IOException e) {
|
---|
38 | progressMonitor.finishTask();
|
---|
39 | if (con.getHeaderField("Error") != null)
|
---|
40 | throw new OsmTransferException(tr(con.getHeaderField("Error")));
|
---|
41 | throw new OsmTransferException(e);
|
---|
42 | }
|
---|
43 |
|
---|
44 | updateSize();
|
---|
45 | if (!sizeKnown) {
|
---|
46 | progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override public void close() throws IOException {
|
---|
51 | try {
|
---|
52 | in.close();
|
---|
53 | } finally {
|
---|
54 | progressMonitor.finishTask();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override public int read(byte[] b, int off, int len) throws IOException {
|
---|
59 | int read = in.read(b, off, len);
|
---|
60 | if (read != -1) {
|
---|
61 | advanceTicker(read);
|
---|
62 | } else {
|
---|
63 | progressMonitor.finishTask();
|
---|
64 | }
|
---|
65 | return read;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override public int read() throws IOException {
|
---|
69 | int read = in.read();
|
---|
70 | if (read != -1) {
|
---|
71 | advanceTicker(1);
|
---|
72 | } else {
|
---|
73 | progressMonitor.finishTask();
|
---|
74 | }
|
---|
75 | return read;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Increase ticker (progress counter and displayed text) by the given amount.
|
---|
80 | * @param amount
|
---|
81 | */
|
---|
82 | private void advanceTicker(int amount) {
|
---|
83 | readSoFar += amount;
|
---|
84 | updateSize();
|
---|
85 |
|
---|
86 | if (readSoFar / 1024 != lastDialogUpdate) {
|
---|
87 | lastDialogUpdate++;
|
---|
88 | if (sizeKnown) {
|
---|
89 | progressMonitor.setTicks(readSoFar);
|
---|
90 | }
|
---|
91 | progressMonitor.setExtraText(readSoFar/1024 + " KB");
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void updateSize() {
|
---|
96 | if (!sizeKnown && connection.getContentLength() > 0) {
|
---|
97 | sizeKnown = true;
|
---|
98 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
99 | progressMonitor.setTicksCount(connection.getContentLength());
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|