Ignore:
Timestamp:
2015-12-28T01:42:36+01:00 (8 years ago)
Author:
simon04
Message:

see #12231 - HttpClient now reports status to ProgressMonitor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java

    r9172 r9185  
    1010import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    1111import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    12 import org.openstreetmap.josm.tools.HttpClient;
    1312
    1413/**
     
    1817public class ProgressInputStream extends InputStream {
    1918
     19    private final StreamProgressUpdater updater;
    2020    private final InputStream in;
    21     private final long size;
    22     private int readSoFar;
    23     private int lastDialogUpdate;
    24     private final ProgressMonitor progressMonitor;
    2521
     22    /**
     23     * Constructs a new {@code ProgressInputStream}.
     24     *
     25     * @param in the stream to monitor
     26     * @param size the total size which will be sent
     27     * @param progressMonitor the monitor to report to
     28     * @since 9172
     29     */
    2630    public ProgressInputStream(InputStream in, long size, ProgressMonitor progressMonitor) {
    2731        if (progressMonitor == null) {
    2832            progressMonitor = NullProgressMonitor.INSTANCE;
    2933        }
     34        this.updater = new StreamProgressUpdater(size, progressMonitor, tr("Downloading data..."));
    3035        this.in = in;
    31         this.size = size;
    32         this.progressMonitor = progressMonitor;
    33         progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
    34         progressMonitor.indeterminateSubTask(null);
    35         initProgressMonitor();
    3636    }
    3737
    38     public ProgressInputStream(HttpClient.Response response, ProgressMonitor progressMonitor) throws IOException {
    39         this(response.getContent(), response.getContentLength(), progressMonitor);
    40     }
    41 
     38    /**
     39     * Constructs a new {@code ProgressInputStream}.
     40     *
     41     * Will call {@link URLConnection#getInputStream()} to obtain the stream to monitor.
     42     *
     43     * @param con the connection to monitor
     44     * @param progressMonitor the monitor to report to
     45     */
    4246    public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
    4347        if (progressMonitor == null) {
    4448            progressMonitor = NullProgressMonitor.INSTANCE;
    4549        }
    46         this.progressMonitor = progressMonitor;
    4750        progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
    4851        progressMonitor.indeterminateSubTask(null);
     
    5053        try {
    5154            this.in = con.getInputStream();
    52             this.size = con.getContentLength();
     55            this.updater = new StreamProgressUpdater(con.getContentLength(), progressMonitor, tr("Downloading data..."));
    5356        } catch (IOException e) {
    5457            progressMonitor.finishTask();
     
    5760            throw new OsmTransferException(e);
    5861        }
    59         initProgressMonitor();
    6062    }
    6163
    62     protected void initProgressMonitor() {
    63         if (size > 0) {
    64             progressMonitor.subTask(tr("Downloading OSM data..."));
    65             progressMonitor.setTicksCount((int) size);
    66         } else {
    67             progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
     64    @Override
     65    public void close() throws IOException {
     66        try {
     67            in.close();
     68        } finally {
     69            updater.finishTask();
    6870        }
    6971    }
    7072
    71     @Override public void close() throws IOException {
    72         try {
    73             in.close();
    74         } finally {
    75             progressMonitor.finishTask();
    76         }
    77     }
    78 
    79     @Override public int read(byte[] b, int off, int len) throws IOException {
     73    @Override
     74    public int read(byte[] b, int off, int len) throws IOException {
    8075        int read = in.read(b, off, len);
    8176        if (read != -1) {
    82             advanceTicker(read);
     77            updater.advanceTicker(read);
    8378        } else {
    84             progressMonitor.finishTask();
     79            updater.finishTask();
    8580        }
    8681        return read;
    8782    }
    8883
    89     @Override public int read() throws IOException {
     84    @Override
     85    public int read() throws IOException {
    9086        int read = in.read();
    9187        if (read != -1) {
    92             advanceTicker(1);
     88            updater.advanceTicker(1);
    9389        } else {
    94             progressMonitor.finishTask();
     90            updater.finishTask();
    9591        }
    9692        return read;
    9793    }
    98 
    99     /**
    100      * Increase ticker (progress counter and displayed text) by the given amount.
    101      * @param amount number of ticks
    102      */
    103     private void advanceTicker(int amount) {
    104         readSoFar += amount;
    105 
    106         if (readSoFar / 1024 != lastDialogUpdate) {
    107             lastDialogUpdate++;
    108             if (size > 0) {
    109                 progressMonitor.setTicks(readSoFar);
    110             }
    111             progressMonitor.setExtraText(readSoFar/1024 + " KB");
    112         }
    113     }
    11494}
Note: See TracChangeset for help on using the changeset viewer.