/** * Provide a description of a file/data size (in bytes). The output shows * the exact number with units ("B"). If the value is larger than 1 KiB * then the scaled value is shown in the most appropriate units. For * example 10 --> "10 B", 2560 --> "2560 B (2.5 KiB)". * @param size The size to represent. * @return a String describing this value. */ public static String describeSize(long size) { StringBuilder sb = new StringBuilder(); sb.append(size).append(' ').append(isoSymbol().of(ByteUnit.BYTES)); ByteUnit units = BINARY.unitsOf(size); if (units != ByteUnit.BYTES) { sb.append(" (").append(toThreeSigFig(units.convert((double)size, ByteUnit.BYTES), 1024)) .append(' ').append(isoSymbol().of(units)).append(')'); } return sb.toString(); }
public long parse(String value, ByteUnit targetUnits) throws NumberFormatException { String whitespaceAndUnit = whitespace.regularExpression + "(?<unit>\\p{Alpha}+)"; String whitespaceAndUnitWithPresence = unitPresence == UnitPresence.REQUIRED ? whitespaceAndUnit : ("(?:" + whitespaceAndUnit + ")?"); Pattern pattern = Pattern.compile("(?<number>" + input.regularExpression + ")" + whitespaceAndUnitWithPresence); Matcher m = pattern.matcher(value); if (!m.matches()) { throw new NumberFormatException("Bad input \"" + value + "\" does not match " + pattern); } ByteUnit givenUnits = Optional.ofNullable(m.group("unit")) .map(s -> representation.parse(s).orElseThrow(() -> new NumberFormatException("Unknown unit \"" + s + "\""))) .orElse(defaultUnits); return input.convert(m.group("number"), givenUnits, targetUnits, coersion); }
double scaledMean = units.convert(mean, ByteUnit.BYTES); double scaledSem = units.convert(sem, ByteUnit.BYTES); meanDescription = "(" + toThreeSigFig(scaledMean, 1024, scaledSem) + ") " + isoSymbol().of(units);
double scaledSem = units.convert(sem, ByteUnit.BYTES); meanDescription = "(" + toThreeSigFig(scaledMean, 1024, scaledSem) + ") " + isoSymbol().of(units) + "/s";
private void render(long value, int actualWidth, PrintWriter out) { ByteUnit units = DECIMAL.unitsOf(value); String symbol = isoSymbol().of(units); String numerical = render(value, units); int padding = actualWidth - numerical.length() - symbol.length(); while (padding-- > 0) { out.append(' '); } out.append(numerical).append(symbol); }
/** * Provide a human-readable description of a bandwidth value, using binary * prefixes (2^10, 2^20, ...) and ISO symbols (KiB/s, MiB/s, ...). A * typical description is 36280729 --> "34.6 MiB/s" * @param value the bandwidth value, in bytes per second * @return a human understandable description of that bandwidth */ public static String describeBandwidth(double value) { ByteUnit units = BINARY.unitsOf(value); return toThreeSigFig(units.convert((double)value, ByteUnit.BYTES), 1024) + " " + isoSymbol().of(units) + "/s"; }
/** * Provide a file/data size (in bytes) a human readable form, using binary units * and ISO symbols. For example, 10 --> "10 B", 2560 --> "2.5 KiB". * @param size The size to represent. * @return a String describing this value. */ public static String humanReadableSize(long size) { ByteUnit units = BINARY.unitsOf(size); return toThreeSigFig(units.convert((double)size, ByteUnit.BYTES), 1024) + " " + isoSymbol().of(units); }