/** * Given a number of bytes return a string formatted into the unit of measure indicated */ public static String getSize(long bytes, Size unitOfMeasure) { Size uom = (unitOfMeasure == null) ? getSizeEnum(bytes) : unitOfMeasure; StringBuilder sb = new StringBuilder(); sb.append(getFormattedSize(bytes, uom)); sb.append(uom.getSizeLabel()); return sb.toString(); }
/** * Given a number of bytes return a string formatted into the unit of measure indicated */ public static String getIntegerSize(long bytes, final Size unitOfMeasure) { Size uom = (unitOfMeasure == null) ? getSizeEnum(bytes) : unitOfMeasure; StringBuilder sb = new StringBuilder(); synchronized (integerFormatter) { sb.append(integerFormatter.format(bytes / (double) uom.getValue())); } sb.append(uom.getSizeLabel()); return sb.toString(); }
/** * Given a number of bytes return a string formatted into the unit of measure indicated */ public static String getSize(long bytes, final Size unitOfMeasure, NumberFormat formatter) { Size uom = (unitOfMeasure == null) ? getSizeEnum(bytes) : unitOfMeasure; StringBuilder sb = new StringBuilder(); synchronized (formatter) { sb.append(formatter.format(bytes / (double) uom.getValue())); } sb.append(uom.getSizeLabel()); return sb.toString(); }
/** * Given a number of bytes and the number of milliseconds it took to transfer that number of bytes, return bytes/s, KB/s, MB/s, GB/s, TB/s, PB/s, or EB/s as appropriate */ public static String getRate(long millis, long bytes, NumberFormat rateFormatter) { double seconds = millis / SECOND; double bytesPerSecond = bytes / seconds; Size bandwidthLevel = getSizeEnum(bytesPerSecond); double transferRate = bytesPerSecond / bandwidthLevel.getValue(); synchronized (rateFormatter) { return rateFormatter.format(transferRate) + " " + bandwidthLevel.getRateLabel(); } }