- Timestamp:
- 2015-12-28T01:42:36+01:00 (9 years ago)
- 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 131 131 132 132 try { 133 activeConnection = client.connect( );133 activeConnection = client.connect(progressMonitor); 134 134 } catch (Exception e) { 135 135 Main.error(e); … … 158 158 159 159 activeConnection.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition); 160 InputStream in = new ProgressInputStream(activeConnection, progressMonitor); 161 return in; 160 return activeConnection.getContent(); 162 161 } catch (OsmTransferException e) { 163 162 throw e; -
trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java
r9172 r9185 10 10 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 11 11 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 12 import org.openstreetmap.josm.tools.HttpClient;13 12 14 13 /** … … 18 17 public class ProgressInputStream extends InputStream { 19 18 19 private final StreamProgressUpdater updater; 20 20 private final InputStream in; 21 private final long size;22 private int readSoFar;23 private int lastDialogUpdate;24 private final ProgressMonitor progressMonitor;25 21 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 */ 26 30 public ProgressInputStream(InputStream in, long size, ProgressMonitor progressMonitor) { 27 31 if (progressMonitor == null) { 28 32 progressMonitor = NullProgressMonitor.INSTANCE; 29 33 } 34 this.updater = new StreamProgressUpdater(size, progressMonitor, tr("Downloading data...")); 30 35 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();36 36 } 37 37 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 */ 42 46 public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException { 43 47 if (progressMonitor == null) { 44 48 progressMonitor = NullProgressMonitor.INSTANCE; 45 49 } 46 this.progressMonitor = progressMonitor;47 50 progressMonitor.beginTask(tr("Contacting OSM Server..."), 1); 48 51 progressMonitor.indeterminateSubTask(null); … … 50 53 try { 51 54 this.in = con.getInputStream(); 52 this. size = con.getContentLength();55 this.updater = new StreamProgressUpdater(con.getContentLength(), progressMonitor, tr("Downloading data...")); 53 56 } catch (IOException e) { 54 57 progressMonitor.finishTask(); … … 57 60 throw new OsmTransferException(e); 58 61 } 59 initProgressMonitor();60 62 } 61 63 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(); 68 70 } 69 71 } 70 72 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 { 80 75 int read = in.read(b, off, len); 81 76 if (read != -1) { 82 advanceTicker(read);77 updater.advanceTicker(read); 83 78 } else { 84 progressMonitor.finishTask();79 updater.finishTask(); 85 80 } 86 81 return read; 87 82 } 88 83 89 @Override public int read() throws IOException { 84 @Override 85 public int read() throws IOException { 90 86 int read = in.read(); 91 87 if (read != -1) { 92 advanceTicker(1);88 updater.advanceTicker(1); 93 89 } else { 94 progressMonitor.finishTask();90 updater.finishTask(); 95 91 } 96 92 return read; 97 93 } 98 99 /**100 * Increase ticker (progress counter and displayed text) by the given amount.101 * @param amount number of ticks102 */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 }114 94 } -
trunk/src/org/openstreetmap/josm/tools/HttpClient.java
r9184 r9185 22 22 import org.openstreetmap.josm.Main; 23 23 import org.openstreetmap.josm.data.Version; 24 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 24 25 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 25 26 import org.openstreetmap.josm.io.Compression; 27 import org.openstreetmap.josm.io.ProgressInputStream; 28 import org.openstreetmap.josm.io.ProgressOutputStream; 26 29 import org.openstreetmap.josm.io.UTFInputStreamReader; 27 30 … … 61 64 /** 62 65 * Opens the HTTP connection. 63 * @param monitor progress monitor66 * @param progressMonitor progress monitor 64 67 * @return HTTP response 65 68 * @throws IOException if any I/O error occurs 66 69 * @since 9179 67 70 */ 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 } 69 75 final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 70 76 connection.setRequestMethod(requestMethod); … … 89 95 } 90 96 91 // FIXME: use ProgressMonitor 97 progressMonitor.beginTask(tr("Contacting Server..."), 1); 98 progressMonitor.indeterminateSubTask(null); 92 99 93 100 if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) { … … 95 102 headers.put("Content-Length", String.valueOf(requestBody.length)); 96 103 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))) { 98 106 out.write(requestBody); 99 107 } … … 137 145 } 138 146 } 139 Response response = new Response(connection );147 Response response = new Response(connection, progressMonitor); 140 148 successfulConnection = true; 141 149 return response; … … 152 160 public static final class Response { 153 161 private final HttpURLConnection connection; 162 private final ProgressMonitor monitor; 154 163 private final int responseCode; 155 164 private final String responseMessage; … … 157 166 private boolean uncompressAccordingToContentDisposition; 158 167 159 private Response(HttpURLConnection connection ) throws IOException {168 private Response(HttpURLConnection connection, ProgressMonitor monitor) throws IOException { 160 169 CheckParameterUtil.ensureParameterNotNull(connection, "connection"); 170 CheckParameterUtil.ensureParameterNotNull(monitor, "monitor"); 161 171 this.connection = connection; 172 this.monitor = monitor; 162 173 this.responseCode = connection.getResponseCode(); 163 174 this.responseMessage = connection.getResponseMessage(); … … 227 238 in = connection.getErrorStream(); 228 239 } 240 in = new ProgressInputStream(in, getContentLength(), monitor); 229 241 in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in; 230 242 Compression compression = Compression.NONE;
Note:
See TracChangeset
for help on using the changeset viewer.