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

Last change on this file since 2751 was 1875, checked in by jttt, 15 years ago

Fix #3134

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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;
12
13/**
14 * Read from an other reader and increment an progress counter while on the way.
15 * @author Imi
16 */
17public 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 in.close();
52 progressMonitor.finishTask();
53 }
54
55 @Override public int read(byte[] b, int off, int len) throws IOException {
56 int read = in.read(b, off, len);
57 if (read != -1) {
58 advanceTicker(read);
59 } else {
60 progressMonitor.finishTask();
61 }
62 return read;
63 }
64
65 @Override public int read() throws IOException {
66 int read = in.read();
67 if (read != -1) {
68 advanceTicker(1);
69 } else {
70 progressMonitor.finishTask();
71 }
72 return read;
73 }
74
75 /**
76 * Increase ticker (progress counter and displayed text) by the given amount.
77 * @param amount
78 */
79 private void advanceTicker(int amount) {
80 readSoFar += amount;
81 updateSize();
82
83 if (readSoFar / 1024 != lastDialogUpdate) {
84 lastDialogUpdate++;
85 if (sizeKnown) {
86 progressMonitor.setExtraText(readSoFar/1024 + " KB");
87 progressMonitor.setTicks(readSoFar);
88 } else {
89 progressMonitor.setExtraText("??? KB");
90 }
91 }
92 }
93
94 private void updateSize() {
95 if (!sizeKnown && connection.getContentLength() > 0) {
96 sizeKnown = true;
97 progressMonitor.subTask(tr("Downloading OSM data..."));
98 progressMonitor.setTicksCount(connection.getContentLength());
99 }
100 }
101}
Note: See TracBrowser for help on using the repository browser.