01: package com.coldcore.coloradoftp.plugin.intellipack.connection;
02:
03: /**
04: * Contains number of bytes transferred by all connections.
05: * This class reset itself every second.
06: */
07: public class Speedometer {
08:
09: protected long bytesLastSecond;
10: protected long bytesTransferredSec;
11: protected long lastSecondTime;
12:
13: public Speedometer() {
14: lastSecondTime = System.currentTimeMillis();
15: }
16:
17: /** Test if this is a time to reset and resets */
18: protected void checkTime() {
19: long currentTime = System.currentTimeMillis();
20: if (currentTime > lastSecondTime + 1000L) {
21: bytesLastSecond = bytesTransferredSec;
22: bytesTransferredSec = 0L;
23: lastSecondTime = currentTime;
24: }
25: }
26:
27: /** Add amount of transferred bytes
28: * @param amount
29: */
30: public void add(long amount) {
31: bytesTransferredSec += amount;
32: }
33:
34: /** Get amount of butes transferred during the current second
35: * @return Bytes transferred by all data connections
36: */
37: public long getBytesThisSecond() {
38: checkTime();
39: return bytesTransferredSec;
40: }
41:
42: /** Get bytes transferred during the last second
43: * @return Bytes per second of all data connections during the last second
44: */
45: public long getLastSecondBytes() {
46: checkTime();
47: return bytesLastSecond;
48: }
49: }
|