[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[782] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[626] | 6 | import java.io.IOException;
|
---|
| 7 | import java.io.InputStream;
|
---|
| 8 | import java.net.URLConnection;
|
---|
[11553] | 9 | import java.util.Optional;
|
---|
[626] | 10 |
|
---|
[1811] | 11 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 12 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[9227] | 13 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[626] | 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Read from an other reader and increment an progress counter while on the way.
|
---|
| 17 | * @author Imi
|
---|
| 18 | */
|
---|
| 19 | public class ProgressInputStream extends InputStream {
|
---|
| 20 |
|
---|
[9185] | 21 | private final StreamProgressUpdater updater;
|
---|
[1169] | 22 | private final InputStream in;
|
---|
[626] | 23 |
|
---|
[9185] | 24 | /**
|
---|
| 25 | * Constructs a new {@code ProgressInputStream}.
|
---|
| 26 | *
|
---|
[9227] | 27 | * @param in the stream to monitor. Must not be null
|
---|
[9185] | 28 | * @param size the total size which will be sent
|
---|
| 29 | * @param progressMonitor the monitor to report to
|
---|
| 30 | * @since 9172
|
---|
| 31 | */
|
---|
[9172] | 32 | public ProgressInputStream(InputStream in, long size, ProgressMonitor progressMonitor) {
|
---|
[9227] | 33 | CheckParameterUtil.ensureParameterNotNull(in, "in");
|
---|
[11553] | 34 | this.updater = new StreamProgressUpdater(size,
|
---|
| 35 | Optional.ofNullable(progressMonitor).orElse(NullProgressMonitor.INSTANCE), tr("Downloading data..."));
|
---|
[9172] | 36 | this.in = in;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[9185] | 39 | /**
|
---|
| 40 | * Constructs a new {@code ProgressInputStream}.
|
---|
| 41 | *
|
---|
| 42 | * Will call {@link URLConnection#getInputStream()} to obtain the stream to monitor.
|
---|
| 43 | *
|
---|
| 44 | * @param con the connection to monitor
|
---|
| 45 | * @param progressMonitor the monitor to report to
|
---|
[9227] | 46 | * @throws OsmTransferException if any I/O error occurs
|
---|
[9185] | 47 | */
|
---|
[1811] | 48 | public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 49 | if (progressMonitor == null) {
|
---|
| 50 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
| 51 | }
|
---|
| 52 | progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
|
---|
| 53 | progressMonitor.indeterminateSubTask(null);
|
---|
[626] | 54 |
|
---|
[1169] | 55 | try {
|
---|
| 56 | this.in = con.getInputStream();
|
---|
[9185] | 57 | this.updater = new StreamProgressUpdater(con.getContentLength(), progressMonitor, tr("Downloading data..."));
|
---|
[1169] | 58 | } catch (IOException e) {
|
---|
[1875] | 59 | progressMonitor.finishTask();
|
---|
[1169] | 60 | if (con.getHeaderField("Error") != null)
|
---|
[8394] | 61 | throw new OsmTransferException(tr(con.getHeaderField("Error")), e);
|
---|
[1670] | 62 | throw new OsmTransferException(e);
|
---|
[1169] | 63 | }
|
---|
[9172] | 64 | }
|
---|
[626] | 65 |
|
---|
[9185] | 66 | @Override
|
---|
| 67 | public void close() throws IOException {
|
---|
[5874] | 68 | try {
|
---|
| 69 | in.close();
|
---|
| 70 | } finally {
|
---|
[9185] | 71 | updater.finishTask();
|
---|
[5874] | 72 | }
|
---|
[1169] | 73 | }
|
---|
[626] | 74 |
|
---|
[9185] | 75 | @Override
|
---|
| 76 | public int read(byte[] b, int off, int len) throws IOException {
|
---|
[1169] | 77 | int read = in.read(b, off, len);
|
---|
[1670] | 78 | if (read != -1) {
|
---|
[9185] | 79 | updater.advanceTicker(read);
|
---|
[1811] | 80 | } else {
|
---|
[9185] | 81 | updater.finishTask();
|
---|
[1670] | 82 | }
|
---|
[1169] | 83 | return read;
|
---|
| 84 | }
|
---|
[626] | 85 |
|
---|
[9185] | 86 | @Override
|
---|
| 87 | public int read() throws IOException {
|
---|
[1169] | 88 | int read = in.read();
|
---|
[1670] | 89 | if (read != -1) {
|
---|
[9185] | 90 | updater.advanceTicker(1);
|
---|
[1811] | 91 | } else {
|
---|
[9185] | 92 | updater.finishTask();
|
---|
[1670] | 93 | }
|
---|
[1169] | 94 | return read;
|
---|
| 95 | }
|
---|
[626] | 96 | }
|
---|