1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.net.URLConnection;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Read from an other reader and increment an progress counter while on the way.
|
---|
11 | * @author Imi
|
---|
12 | */
|
---|
13 | public 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 | private PleaseWaitDialog pleaseWaitDlg;
|
---|
20 |
|
---|
21 | public class OsmServerException extends IOException {
|
---|
22 | private OsmServerException(String e) {
|
---|
23 | super(e);
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | public ProgressInputStream(URLConnection con, PleaseWaitDialog pleaseWaitDlg) throws IOException, OsmServerException {
|
---|
28 | this.connection = con;
|
---|
29 |
|
---|
30 | try {
|
---|
31 | this.in = con.getInputStream();
|
---|
32 | } catch (IOException e) {
|
---|
33 | if (con.getHeaderField("Error") != null)
|
---|
34 | throw new OsmServerException(con.getHeaderField("Error"));
|
---|
35 | throw e;
|
---|
36 | }
|
---|
37 |
|
---|
38 | int contentLength = con.getContentLength();
|
---|
39 | this.pleaseWaitDlg = pleaseWaitDlg;
|
---|
40 | if (pleaseWaitDlg == null)
|
---|
41 | return;
|
---|
42 | if (contentLength > 0)
|
---|
43 | pleaseWaitDlg.progress.setMaximum(contentLength);
|
---|
44 | else
|
---|
45 | pleaseWaitDlg.progress.setMaximum(0);
|
---|
46 | pleaseWaitDlg.progress.setValue(0);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public void close() throws IOException {
|
---|
50 | in.close();
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override public int read(byte[] b, int off, int len) throws IOException {
|
---|
54 | int read = in.read(b, off, len);
|
---|
55 | if (read != -1)
|
---|
56 | advanceTicker(read);
|
---|
57 | return read;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override public int read() throws IOException {
|
---|
61 | int read = in.read();
|
---|
62 | if (read != -1)
|
---|
63 | advanceTicker(1);
|
---|
64 | return read;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Increase ticker (progress counter and displayed text) by the given amount.
|
---|
69 | * @param amount
|
---|
70 | */
|
---|
71 | private void advanceTicker(int amount) {
|
---|
72 | if (pleaseWaitDlg == null)
|
---|
73 | return;
|
---|
74 |
|
---|
75 | if (pleaseWaitDlg.progress.getMaximum() == 0 && connection.getContentLength() != -1)
|
---|
76 | pleaseWaitDlg.progress.setMaximum(connection.getContentLength());
|
---|
77 |
|
---|
78 | readSoFar += amount;
|
---|
79 |
|
---|
80 | if (readSoFar / 1024 != lastDialogUpdate) {
|
---|
81 | lastDialogUpdate++;
|
---|
82 | String progStr = " "+readSoFar/1024+"/";
|
---|
83 | progStr += (pleaseWaitDlg.progress.getMaximum()==0) ? "??? KB" : (pleaseWaitDlg.progress.getMaximum()/1024)+" KB";
|
---|
84 | pleaseWaitDlg.progress.setValue(readSoFar);
|
---|
85 |
|
---|
86 | String cur = pleaseWaitDlg.currentAction.getText();
|
---|
87 | int i = cur.indexOf(' ');
|
---|
88 | if (i != -1)
|
---|
89 | cur = cur.substring(0, i) + progStr;
|
---|
90 | else
|
---|
91 | cur += progStr;
|
---|
92 | pleaseWaitDlg.currentAction.setText(cur);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|