source: josm/src/org/openstreetmap/josm/io/ProgressInputStream.java@ 153

Last change on this file since 153 was 153, checked in by imi, 19 years ago
  • added possibility to create new download tasks (download data types).
  • removed WMS stuff (now available as landsat - plugin)
  • updated translation files
File size: 2.1 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.URLConnection;
6
7import org.openstreetmap.josm.Main;
8
9/**
10 * Read from an other reader and increment an progress counter while on the way.
11 * @author Imi
12 */
13public class ProgressInputStream extends InputStream {
14
15 private final InputStream in;
16 private int readSoFar = 0;
17 private int lastDialogUpdate = 0;
18 private final URLConnection connection;
19
20 public ProgressInputStream(URLConnection con) throws IOException {
21 this.connection = con;
22 this.in = con.getInputStream();
23 int contentLength = con.getContentLength();
24 if (contentLength > 0)
25 Main.pleaseWaitDlg.progress.setMaximum(contentLength);
26 else
27 Main.pleaseWaitDlg.progress.setMaximum(0);
28 Main.pleaseWaitDlg.progress.setValue(0);
29 }
30
31 @Override public void close() throws IOException {
32 in.close();
33 }
34
35 @Override public int read(byte[] b, int off, int len) throws IOException {
36 int read = in.read(b, off, len);
37 if (read != -1)
38 advanceTicker(read);
39 return read;
40 }
41
42 @Override public int read() throws IOException {
43 int read = in.read();
44 if (read != -1)
45 advanceTicker(1);
46 return read;
47 }
48
49 /**
50 * Increase ticker (progress counter and displayed text) by the given amount.
51 * @param amount
52 */
53 private void advanceTicker(int amount) {
54 if (Main.pleaseWaitDlg.progress.getMaximum() == 0 && connection.getContentLength() != -1)
55 Main.pleaseWaitDlg.progress.setMaximum(connection.getContentLength());
56
57 readSoFar += amount;
58
59 if (readSoFar / 1024 != lastDialogUpdate) {
60 lastDialogUpdate++;
61 String progStr = " "+readSoFar/1024+"/";
62 progStr += (Main.pleaseWaitDlg.progress.getMaximum()==0) ? "??? KB" : (Main.pleaseWaitDlg.progress.getMaximum()/1024)+" KB";
63 Main.pleaseWaitDlg.progress.setValue(readSoFar);
64
65 String cur = Main.pleaseWaitDlg.currentAction.getText();
66 int i = cur.indexOf(' ');
67 if (i != -1)
68 cur = cur.substring(0, i) + progStr;
69 else
70 cur += progStr;
71 Main.pleaseWaitDlg.currentAction.setText(cur);
72 }
73 }
74}
Note: See TracBrowser for help on using the repository browser.