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

Last change on this file since 1814 was 1811, checked in by jttt, 15 years ago

PleaseWait refactoring. Progress is now reported using ProgressMonitor interface, that is available through PleaseWaitRunnable.

  • Property svn:eol-style set to native
File size: 3.0 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 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}
Note: See TracBrowser for help on using the repository browser.