Changeset 9185 in josm for trunk/src


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

see #12231 - HttpClient now reports status to ProgressMonitor

Location:
trunk/src/org/openstreetmap/josm
Files:
2 added
3 edited

Legend:

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

    r9184 r9185  
    131131
    132132            try {
    133                 activeConnection = client.connect();
     133                activeConnection = client.connect(progressMonitor);
    134134            } catch (Exception e) {
    135135                Main.error(e);
     
    158158
    159159                activeConnection.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);
    160                 InputStream in = new ProgressInputStream(activeConnection, progressMonitor);
    161                 return in;
     160                return activeConnection.getContent();
    162161            } catch (OsmTransferException e) {
    163162                throw e;
  • 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}
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9184 r9185  
    2222import org.openstreetmap.josm.Main;
    2323import org.openstreetmap.josm.data.Version;
     24import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2425import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2526import org.openstreetmap.josm.io.Compression;
     27import org.openstreetmap.josm.io.ProgressInputStream;
     28import org.openstreetmap.josm.io.ProgressOutputStream;
    2629import org.openstreetmap.josm.io.UTFInputStreamReader;
    2730
     
    6164    /**
    6265     * Opens the HTTP connection.
    63      * @param monitor progress monitor
     66     * @param progressMonitor progress monitor
    6467     * @return HTTP response
    6568     * @throws IOException if any I/O error occurs
    6669     * @since 9179
    6770     */
    68     public Response connect(ProgressMonitor monitor) throws IOException {
     71    public Response connect(ProgressMonitor progressMonitor) throws IOException {
     72        if (progressMonitor == null) {
     73            progressMonitor = NullProgressMonitor.INSTANCE;
     74        }
    6975        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    7076        connection.setRequestMethod(requestMethod);
     
    8995        }
    9096
    91         // FIXME: use ProgressMonitor
     97        progressMonitor.beginTask(tr("Contacting Server..."), 1);
     98        progressMonitor.indeterminateSubTask(null);
    9299
    93100        if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) {
     
    95102            headers.put("Content-Length", String.valueOf(requestBody.length));
    96103            connection.setDoOutput(true);
    97             try (OutputStream out = new BufferedOutputStream(connection.getOutputStream())) {
     104            try (OutputStream out = new BufferedOutputStream(
     105                    new ProgressOutputStream(connection.getOutputStream(), requestBody.length, progressMonitor))) {
    98106                out.write(requestBody);
    99107            }
     
    137145                }
    138146            }
    139             Response response = new Response(connection);
     147            Response response = new Response(connection, progressMonitor);
    140148            successfulConnection = true;
    141149            return response;
     
    152160    public static final class Response {
    153161        private final HttpURLConnection connection;
     162        private final ProgressMonitor monitor;
    154163        private final int responseCode;
    155164        private final String responseMessage;
     
    157166        private boolean uncompressAccordingToContentDisposition;
    158167
    159         private Response(HttpURLConnection connection) throws IOException {
     168        private Response(HttpURLConnection connection, ProgressMonitor monitor) throws IOException {
    160169            CheckParameterUtil.ensureParameterNotNull(connection, "connection");
     170            CheckParameterUtil.ensureParameterNotNull(monitor, "monitor");
    161171            this.connection = connection;
     172            this.monitor = monitor;
    162173            this.responseCode = connection.getResponseCode();
    163174            this.responseMessage = connection.getResponseMessage();
     
    227238                in = connection.getErrorStream();
    228239            }
     240            in = new ProgressInputStream(in, getContentLength(), monitor);
    229241            in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
    230242            Compression compression = Compression.NONE;
Note: See TracChangeset for help on using the changeset viewer.