@Override protected long convert(String givenValue, ByteUnit givenUnits, ByteUnit targetUnits, Coercion coersion) { return targetUnits.convert(Long.parseLong(givenValue), givenUnits); } },
private String render(long value, ByteUnit units) { if (units == BYTES) { return String.format("%3d", value); } else { double tmp = units.convert((double) value, BYTES); if (tmp >= 0 && tmp < 9.95) { return String.format("%.1f", tmp); } else { return String.format("%.0f", tmp); } } }
public int convert(int sourceValue, ByteUnit sourcePrefix) { if (sourceValue == Integer.MAX_VALUE) { return sourceValue; } return checkedCast(convert((long) sourceValue, sourcePrefix)); }
@Override protected long convert(String givenValue, ByteUnit givenUnits, ByteUnit targetUnits, Coercion coersion) { double value = Double.parseDouble(givenValue); return coersion.toLong(targetUnits.convert(value, givenUnits)); } };
/** * 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(); }
/** * 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); }
/** * 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"; }