From 10e2c3832d8bfddf6b2c7d66df949b0f3571006c Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Thu, 22 Jan 2009 04:02:41 +0000 Subject: [PATCH] Move SummaryHelper.getTransferred() to DataHelper, rename to formatSize(), use on tunnels.jsp --- .../src/net/i2p/router/web/SummaryHelper.java | 58 +++++-------------- core/java/src/net/i2p/data/DataHelper.java | 26 ++++++++- .../router/tunnel/pool/TunnelPoolManager.java | 5 +- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/apps/routerconsole/java/src/net/i2p/router/web/SummaryHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/SummaryHelper.java index 2b445a004..ad8e7135d 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/SummaryHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/SummaryHelper.java @@ -244,7 +244,7 @@ public class SummaryHelper { */ public String getInboundSecondKBps() { if (_context == null) - return "0.0"; + return "0"; double kbps = _context.bandwidthLimiter().getReceiveBps()/1024d; DecimalFormat fmt = new DecimalFormat("##0.00"); return fmt.format(kbps); @@ -256,7 +256,7 @@ public class SummaryHelper { */ public String getOutboundSecondKBps() { if (_context == null) - return "0.0"; + return "0"; double kbps = _context.bandwidthLimiter().getSendBps()/1024d; DecimalFormat fmt = new DecimalFormat("##0.00"); return fmt.format(kbps); @@ -269,10 +269,10 @@ public class SummaryHelper { */ public String getInboundFiveMinuteKBps() { if (_context == null) - return "0.0"; + return "0"; RateStat receiveRate = _context.statManager().getRate("bw.recvRate"); - if (receiveRate == null) return "0.0"; + if (receiveRate == null) return "0"; Rate rate = receiveRate.getRate(5*60*1000); double kbps = rate.getAverageValue()/1024; DecimalFormat fmt = new DecimalFormat("##0.00"); @@ -286,10 +286,10 @@ public class SummaryHelper { */ public String getOutboundFiveMinuteKBps() { if (_context == null) - return "0.0"; + return "0"; RateStat receiveRate = _context.statManager().getRate("bw.sendRate"); - if (receiveRate == null) return "0.0"; + if (receiveRate == null) return "0"; Rate rate = receiveRate.getRate(5*60*1000); double kbps = rate.getAverageValue()/1024; DecimalFormat fmt = new DecimalFormat("##0.00"); @@ -303,10 +303,10 @@ public class SummaryHelper { */ public String getInboundLifetimeKBps() { if (_context == null) - return "0.0"; + return "0"; RateStat receiveRate = _context.statManager().getRate("bw.recvRate"); - if (receiveRate == null) return "0.0"; + if (receiveRate == null) return "0"; double kbps = receiveRate.getLifetimeAverageValue()/1024; DecimalFormat fmt = new DecimalFormat("##0.00"); return fmt.format(kbps); @@ -319,10 +319,10 @@ public class SummaryHelper { */ public String getOutboundLifetimeKBps() { if (_context == null) - return "0.0"; + return "0"; RateStat sendRate = _context.statManager().getRate("bw.sendRate"); - if (sendRate == null) return "0.0"; + if (sendRate == null) return "0"; double kbps = sendRate.getLifetimeAverageValue()/1024; DecimalFormat fmt = new DecimalFormat("##0.00"); return fmt.format(kbps); @@ -335,11 +335,11 @@ public class SummaryHelper { */ public String getInboundTransferred() { if (_context == null) - return "0.0"; + return "0"; long received = _context.bandwidthLimiter().getTotalAllocatedInboundBytes(); - return getTransferred(received); + return DataHelper.formatSize(received) + 'B'; } /** @@ -349,40 +349,10 @@ public class SummaryHelper { */ public String getOutboundTransferred() { if (_context == null) - return "0.0"; + return "0"; long sent = _context.bandwidthLimiter().getTotalAllocatedOutboundBytes(); - return getTransferred(sent); - } - - private static String getTransferred(long bytes) { - double val = bytes; - int scale = 0; - if (bytes > 1024*1024*1024) { - // gigs transferred - scale = 3; - val /= (double)(1024*1024*1024); - } else if (bytes > 1024*1024) { - // megs transferred - scale = 2; - val /= (double)(1024*1024); - } else if (bytes > 1024) { - // kbytes transferred - scale = 1; - val /= (double)1024; - } else { - scale = 0; - } - - DecimalFormat fmt = new DecimalFormat("##0.00"); - - String str = fmt.format(val); - switch (scale) { - case 1: return str + "KB"; - case 2: return str + "MB"; - case 3: return str + "GB"; - default: return bytes + "bytes"; - } + return DataHelper.formatSize(sent) + 'B'; } /** diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 835e6a0dd..4a074f17c 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -25,6 +25,7 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.math.BigInteger; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -235,7 +236,7 @@ public class DataHelper { int split = line.indexOf('='); if (split <= 0) continue; String key = line.substring(0, split); - String val = line.substring(split+1); + String val = line.substring(split+1); //.trim() ?????????????? // Unescape line breaks after loading. // Remember: "\" needs escaping both for regex and string. val = val.replaceAll("\\\\r","\r"); @@ -842,6 +843,29 @@ public class DataHelper { } } + /** + * Caller should append 'B' or 'b' as appropriate + */ + public static String formatSize(long bytes) { + double val = bytes; + int scale = 0; + while (val >= 1024) { + scale++; + val /= 1024; + } + + DecimalFormat fmt = new DecimalFormat("##0.00"); + + String str = fmt.format(val); + switch (scale) { + case 1: return str + "K"; + case 2: return str + "M"; + case 3: return str + "G"; + case 4: return str + "T"; + default: return bytes + ""; + } + } + /** * Strip out any HTML (simply removing any less than / greater than symbols) */ diff --git a/router/java/src/net/i2p/router/tunnel/pool/TunnelPoolManager.java b/router/java/src/net/i2p/router/tunnel/pool/TunnelPoolManager.java index 1a3e0d1b6..43120d0b0 100644 --- a/router/java/src/net/i2p/router/tunnel/pool/TunnelPoolManager.java +++ b/router/java/src/net/i2p/router/tunnel/pool/TunnelPoolManager.java @@ -507,7 +507,7 @@ public class TunnelPoolManager implements TunnelManagerFacade { } out.write("</table>\n"); out.write("Inactive participating tunnels: " + inactive + "<br />\n"); - out.write("Lifetime bandwidth usage: " + processed + "KB<br />\n"); + out.write("Lifetime bandwidth usage: " + DataHelper.formatSize(processed*1024) + "B<br />\n"); } class TunnelComparator implements Comparator { @@ -577,7 +577,8 @@ public class TunnelPoolManager implements TunnelManagerFacade { } if (live <= 0) out.write("<b>No tunnels, waiting for the grace period to end</b><br />\n"); - out.write("Lifetime bandwidth usage: " + processedIn + "KB in, " + processedOut + "KB out<br />"); + out.write("Lifetime bandwidth usage: " + DataHelper.formatSize(processedIn*1024) + "B in, " + + DataHelper.formatSize(processedOut*1024) + "B out<br />"); } private String getCapacity(Hash peer) { -- GitLab