Ignore:
Timestamp:
2006-07-12T17:22:23+02:00 (18 years ago)
Author:
imi
Message:
  • fixed performance bug in progress counter (thanks SteveC)
  • added build.xml for ant-freaks. Be sure to have junit.jar in your CLASSPATH (thanks SteveC)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/io/ProgressReader.java

    r103 r109  
    33import java.io.IOException;
    44import java.io.InputStream;
    5 import java.io.InputStreamReader;
    6 import java.io.Reader;
    75import java.net.URLConnection;
    86
     
    1614public class ProgressReader extends InputStream {
    1715
    18         private final Reader in;
     16        private final InputStream in;
    1917        private final BoundedRangeModel progress;
    2018        private final JLabel currentAction;
    2119        private int readSoFar = 0;
     20        private int lastDialogUpdate = 0;
     21        private final URLConnection connection;
    2222
    2323        public ProgressReader(URLConnection con, BoundedRangeModel progress, JLabel currentAction) throws IOException {
    24                 this.in = new InputStreamReader(con.getInputStream());
     24                this.connection = con;
     25                this.in = con.getInputStream();
    2526                this.progress = progress;
    2627                this.currentAction = currentAction;
     
    3132                        progress.setMaximum(0);
    3233                progress.setValue(0);
    33     }
     34        }
    3435
    3536        @Override public void close() throws IOException {
     
    3738        }
    3839
     40        @Override public int read(byte[] b, int off, int len) throws IOException {
     41                int read = in.read(b, off, len);
     42                if (read != -1)
     43                        advanceTicker(read);
     44                return read;
     45        }
     46
    3947        @Override public int read() throws IOException {
    4048                int read = in.read();
    41                 readSoFar++;
     49                if (read != -1)
     50                        advanceTicker(1);
     51                return read;
     52        }
    4253
    43                 String progStr = " ("+readSoFar+"/";
    44                 if (progress.getMaximum() == 0)
    45                         progStr += "???)";
    46                 else
    47                         progStr += progress.getMaximum()+")";
    48                
    49                 String cur = currentAction.getText();
    50                 int i = cur.indexOf(' ');
    51                 if (i != -1)
    52                         cur = cur.substring(0, i) + progStr;
    53                 else
    54                         cur += progStr;
    55                 currentAction.setText(cur);
    56                 progress.setValue(readSoFar);
    57                 return read;
    58     }
     54        /**
     55         * Increase ticker (progress counter and displayed text) by the given amount.
     56         * @param amount
     57         */
     58        private void advanceTicker(int amount) {
     59                if (progress.getMaximum() == 0 && connection.getContentLength() != -1)
     60                        progress.setMaximum(connection.getContentLength());
     61
     62                readSoFar += amount;
     63
     64                if (readSoFar / 1024 != lastDialogUpdate) {
     65                        lastDialogUpdate++;
     66                        String progStr = " "+readSoFar/1024+"/";
     67                        progStr += (progress.getMaximum()==0) ? "??? KB" : (progress.getMaximum()/1024)+" KB";
     68                        progress.setValue(readSoFar);
     69
     70                        String cur = currentAction.getText();
     71                        int i = cur.indexOf(' ');
     72                        if (i != -1)
     73                                cur = cur.substring(0, i) + progStr;
     74                        else
     75                                cur += progStr;
     76                        currentAction.setText(cur);
     77                }
     78        }
    5979}
Note: See TracChangeset for help on using the changeset viewer.