Move SummaryHelper.getTransferred() to DataHelper, rename to formatSize(), use on tunnels.jsp

This commit is contained in:
zzz
2009-01-22 04:02:41 +00:00
parent c620420a6f
commit 10e2c3832d
3 changed files with 42 additions and 47 deletions

View File

@ -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)
*/