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

Last change on this file since 8073 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 3.0 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.io;
3
[782]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[626]6import java.io.IOException;
7import java.io.InputStream;
8import java.net.URLConnection;
9
[1811]10import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
11import org.openstreetmap.josm.gui.progress.ProgressMonitor;
[626]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
[1169]19 private final InputStream in;
20 private int readSoFar = 0;
21 private int lastDialogUpdate = 0;
[1811]22 private boolean sizeKnown;
[1169]23 private final URLConnection connection;
[1811]24 private final ProgressMonitor progressMonitor;
[626]25
[1811]26 public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
[1169]27 this.connection = con;
[1811]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);
[626]34
[1169]35 try {
36 this.in = con.getInputStream();
37 } catch (IOException e) {
[1875]38 progressMonitor.finishTask();
[1169]39 if (con.getHeaderField("Error") != null)
[1670]40 throw new OsmTransferException(tr(con.getHeaderField("Error")));
41 throw new OsmTransferException(e);
[1169]42 }
[626]43
[1811]44 updateSize();
45 if (!sizeKnown) {
46 progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
[1670]47 }
[1169]48 }
[626]49
[1169]50 @Override public void close() throws IOException {
[5874]51 try {
52 in.close();
53 } finally {
54 progressMonitor.finishTask();
55 }
[1169]56 }
[626]57
[1169]58 @Override public int read(byte[] b, int off, int len) throws IOException {
59 int read = in.read(b, off, len);
[1670]60 if (read != -1) {
[1169]61 advanceTicker(read);
[1811]62 } else {
63 progressMonitor.finishTask();
[1670]64 }
[1169]65 return read;
66 }
[626]67
[1169]68 @Override public int read() throws IOException {
69 int read = in.read();
[1670]70 if (read != -1) {
[1169]71 advanceTicker(1);
[1811]72 } else {
73 progressMonitor.finishTask();
[1670]74 }
[1169]75 return read;
76 }
[626]77
[1169]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;
[1811]84 updateSize();
[626]85
[1169]86 if (readSoFar / 1024 != lastDialogUpdate) {
87 lastDialogUpdate++;
[1811]88 if (sizeKnown) {
89 progressMonitor.setTicks(readSoFar);
[1670]90 }
[3005]91 progressMonitor.setExtraText(readSoFar/1024 + " KB");
[1169]92 }
93 }
[1811]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 }
[626]102}
Note: See TracBrowser for help on using the repository browser.