Changeset 9185 in josm for trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java
- Timestamp:
- 2015-12-28T01:42:36+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.