Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / io / LoggingInputStream.java
blobe77a5928d2e9403850ae2706501a06a2cded3c61
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005, 2006 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.io;
25 import java.io.BufferedInputStream;
26 import java.io.InputStream;
27 import java.io.IOException;
30 /** A class to monitor an input stream for speed and total byte download. */
31 public class LoggingInputStream extends BufferedInputStream {
33 private long last;
34 private long n = 0;
35 private long accum_time = 0;
36 private long accum_bytes = 0;
38 public LoggingInputStream(InputStream in) {
39 super(in);
40 last = System.currentTimeMillis();
43 public int read() throws IOException {
44 int m = super.read();
45 n += m;
46 return m;
49 public int read(byte[] b) throws IOException {
50 int m = super.read(b);
51 n += m;
52 return m;
55 public int read(byte[] b, int off, int len) throws IOException {
56 int m = super.read(b, off, len);
57 n += m;
58 return m;
61 /** Put the counter to zero. */
62 public void resetInfo() { // to work perfect, this would need a synchronized clause, but no such perfection is needed, and there are perfomance issues.
63 accum_bytes = n = 0;
64 last = System.currentTimeMillis();
65 accum_time = 0;
68 /** Returns info as
69 * [0] = current time in ms
70 * [1] = elapsed time in ms since last call to getInfo(long[])
71 * [2] = n_bytes_read since last call to getInfo(long[])
72 * [3] = accumulated time in ms since last call to resetInfo()
73 * [4] = accumulated bytes since last call to resetInfo()
75 * So current speed = info[2]/info[1] Kb/s
77 public void getInfo(long[] info) {
78 long now = System.currentTimeMillis();
79 accum_time += now - last;
80 accum_bytes += n;
81 info[0] = now;
82 info[1] = now - last; // elapsed time
83 info[2] = n;
84 info[3] = accum_time; // total time since last call to resetInfo()
85 info[4] = accum_bytes; // total bytes since last call to resetInfo()
86 // reset cycle vars:
87 n = 0;
88 last = now;