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

Last change on this file since 1728 was 1670, checked in by Gubaer, 15 years ago

fixed: bug in OsmApi.getOsmApi()
cleanup: exception handling in interfacing with OSM API
new: new action for updating individual elements with the their current state on the server (including new menu item in the file menu)
new: improved user feedback in case of conflicts
new: handles 410 Gone conflicts when uploading a changeset
new: undoable command for "purging" a primitive from the current dataset (necessary if the primitive is already deleted on the server and the user wants to remove it from its local dataset)
new: undoable command for "undeleting" an already deleted primitive on the server (kind of "cloning")
new: after a full upload, checks whether there are primitives in the local dataset which might be deleted on the server.
new: data structures for history data
new: history download support in io package

  • Property svn:eol-style set to native
File size: 2.9 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 ProgressInputStream(URLConnection con, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException {
25 this.connection = con;
26
27 try {
28 this.in = con.getInputStream();
29 } catch (IOException e) {
30 if (con.getHeaderField("Error") != null)
31 throw new OsmTransferException(tr(con.getHeaderField("Error")));
32 throw new OsmTransferException(e);
33 }
34
35 int contentLength = con.getContentLength();
36 this.pleaseWaitDlg = pleaseWaitDlg;
37 if (pleaseWaitDlg == null)
38 return;
39 if (contentLength > 0) {
40 pleaseWaitDlg.progress.setMaximum(contentLength);
41 } else {
42 pleaseWaitDlg.progress.setMaximum(0);
43 }
44 pleaseWaitDlg.progress.setValue(0);
45 }
46
47 @Override public void close() throws IOException {
48 in.close();
49 }
50
51 @Override public int read(byte[] b, int off, int len) throws IOException {
52 int read = in.read(b, off, len);
53 if (read != -1) {
54 advanceTicker(read);
55 }
56 return read;
57 }
58
59 @Override public int read() throws IOException {
60 int read = in.read();
61 if (read != -1) {
62 advanceTicker(1);
63 }
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
79 readSoFar += amount;
80
81 if (readSoFar / 1024 != lastDialogUpdate) {
82 lastDialogUpdate++;
83 String progStr = " "+readSoFar/1024+"/";
84 progStr += (pleaseWaitDlg.progress.getMaximum()==0) ? "??? KB" : (pleaseWaitDlg.progress.getMaximum()/1024)+" KB";
85 pleaseWaitDlg.progress.setValue(readSoFar);
86
87 String cur = pleaseWaitDlg.currentAction.getText();
88 int i = cur.indexOf(' ');
89 if (i != -1) {
90 cur = cur.substring(0, i) + progStr;
91 } else {
92 cur += progStr;
93 }
94 pleaseWaitDlg.currentAction.setText(cur);
95 }
96 }
97}
Note: See TracBrowser for help on using the repository browser.