| 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 | 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.setTicks(readSoFar); |
|---|
| 87 | } |
|---|
| 88 | progressMonitor.setExtraText(readSoFar/1024 + " KB"); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | private void updateSize() { |
|---|
| 93 | if (!sizeKnown && connection.getContentLength() > 0) { |
|---|
| 94 | sizeKnown = true; |
|---|
| 95 | progressMonitor.subTask(tr("Downloading OSM data...")); |
|---|
| 96 | progressMonitor.setTicksCount(connection.getContentLength()); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|