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

Last change on this file since 1388 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

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