1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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 | if (con.getHeaderField("Error") != null)
|
---|
39 | throw new OsmTransferException(tr(con.getHeaderField("Error")));
|
---|
40 | throw new OsmTransferException(e);
|
---|
41 | }
|
---|
42 |
|
---|
43 | updateSize();
|
---|
44 | if (!sizeKnown) {
|
---|
45 | progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public void close() throws IOException {
|
---|
50 | in.close();
|
---|
51 | progressMonitor.finishTask();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public int read(byte[] b, int off, int len) throws IOException {
|
---|
55 | int read = in.read(b, off, len);
|
---|
56 | if (read != -1) {
|
---|
57 | advanceTicker(read);
|
---|
58 | } else {
|
---|
59 | progressMonitor.finishTask();
|
---|
60 | }
|
---|
61 | return read;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override public int read() throws IOException {
|
---|
65 | int read = in.read();
|
---|
66 | if (read != -1) {
|
---|
67 | advanceTicker(1);
|
---|
68 | } else {
|
---|
69 | progressMonitor.finishTask();
|
---|
70 | }
|
---|
71 | return read;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Increase ticker (progress counter and displayed text) by the given amount.
|
---|
76 | * @param amount
|
---|
77 | */
|
---|
78 | private void advanceTicker(int amount) {
|
---|
79 | readSoFar += amount;
|
---|
80 | updateSize();
|
---|
81 |
|
---|
82 | if (readSoFar / 1024 != lastDialogUpdate) {
|
---|
83 | lastDialogUpdate++;
|
---|
84 | if (sizeKnown) {
|
---|
85 | progressMonitor.setExtraText(readSoFar/1024 + " KB");
|
---|
86 | progressMonitor.setTicks(readSoFar);
|
---|
87 | } else {
|
---|
88 | progressMonitor.setExtraText("??? KB");
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private void updateSize() {
|
---|
94 | if (!sizeKnown && connection.getContentLength() > 0) {
|
---|
95 | sizeKnown = true;
|
---|
96 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
97 | progressMonitor.setTicksCount(connection.getContentLength());
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|