[92] | 1 | package org.openstreetmap.josm.io;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
[103] | 4 | import java.io.InputStream;
|
---|
[93] | 5 | import java.net.URLConnection;
|
---|
[92] | 6 |
|
---|
[160] | 7 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
[92] | 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Read from an other reader and increment an progress counter while on the way.
|
---|
| 11 | * @author Imi
|
---|
| 12 | */
|
---|
[115] | 13 | public class ProgressInputStream extends InputStream {
|
---|
[92] | 14 |
|
---|
[109] | 15 | private final InputStream in;
|
---|
[101] | 16 | private int readSoFar = 0;
|
---|
[109] | 17 | private int lastDialogUpdate = 0;
|
---|
| 18 | private final URLConnection connection;
|
---|
[160] | 19 | private PleaseWaitDialog pleaseWaitDlg;
|
---|
[92] | 20 |
|
---|
[216] | 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 {
|
---|
[109] | 28 | this.connection = con;
|
---|
[216] | 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 |
|
---|
[92] | 38 | int contentLength = con.getContentLength();
|
---|
[160] | 39 | this.pleaseWaitDlg = pleaseWaitDlg;
|
---|
| 40 | if (pleaseWaitDlg == null)
|
---|
| 41 | return;
|
---|
[92] | 42 | if (contentLength > 0)
|
---|
[160] | 43 | pleaseWaitDlg.progress.setMaximum(contentLength);
|
---|
[101] | 44 | else
|
---|
[160] | 45 | pleaseWaitDlg.progress.setMaximum(0);
|
---|
| 46 | pleaseWaitDlg.progress.setValue(0);
|
---|
[109] | 47 | }
|
---|
[92] | 48 |
|
---|
| 49 | @Override public void close() throws IOException {
|
---|
| 50 | in.close();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[109] | 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 |
|
---|
[103] | 60 | @Override public int read() throws IOException {
|
---|
| 61 | int read = in.read();
|
---|
[109] | 62 | if (read != -1)
|
---|
| 63 | advanceTicker(1);
|
---|
| 64 | return read;
|
---|
| 65 | }
|
---|
[101] | 66 |
|
---|
[109] | 67 | /**
|
---|
| 68 | * Increase ticker (progress counter and displayed text) by the given amount.
|
---|
| 69 | * @param amount
|
---|
| 70 | */
|
---|
| 71 | private void advanceTicker(int amount) {
|
---|
[160] | 72 | if (pleaseWaitDlg == null)
|
---|
| 73 | return;
|
---|
[109] | 74 |
|
---|
[160] | 75 | if (pleaseWaitDlg.progress.getMaximum() == 0 && connection.getContentLength() != -1)
|
---|
| 76 | pleaseWaitDlg.progress.setMaximum(connection.getContentLength());
|
---|
| 77 |
|
---|
[109] | 78 | readSoFar += amount;
|
---|
| 79 |
|
---|
| 80 | if (readSoFar / 1024 != lastDialogUpdate) {
|
---|
| 81 | lastDialogUpdate++;
|
---|
| 82 | String progStr = " "+readSoFar/1024+"/";
|
---|
[160] | 83 | progStr += (pleaseWaitDlg.progress.getMaximum()==0) ? "??? KB" : (pleaseWaitDlg.progress.getMaximum()/1024)+" KB";
|
---|
| 84 | pleaseWaitDlg.progress.setValue(readSoFar);
|
---|
[109] | 85 |
|
---|
[160] | 86 | String cur = pleaseWaitDlg.currentAction.getText();
|
---|
[109] | 87 | int i = cur.indexOf(' ');
|
---|
| 88 | if (i != -1)
|
---|
| 89 | cur = cur.substring(0, i) + progStr;
|
---|
| 90 | else
|
---|
| 91 | cur += progStr;
|
---|
[160] | 92 | pleaseWaitDlg.currentAction.setText(cur);
|
---|
[109] | 93 | }
|
---|
| 94 | }
|
---|
[92] | 95 | }
|
---|