19 package org.sleuthkit.autopsy.datasourcesummary.ui;
21 import java.text.DecimalFormat;
22 import org.openide.util.NbBundle;
38 "SizeRepresentationUtil_units_bytes=bytes",
39 "SizeRepresentationUtil_units_kilobytes=KB",
40 "SizeRepresentationUtil_units_megabytes=MB",
41 "SizeRepresentationUtil_units_gigabytes=GB",
42 "SizeRepresentationUtil_units_terabytes=TB",
43 "SizeRepresentationUtil_units_petabytes=PB"
46 BYTES(Bundle.SizeRepresentationUtil_units_bytes(), 0),
47 KB(Bundle.SizeRepresentationUtil_units_kilobytes(), 1),
48 MB(Bundle.SizeRepresentationUtil_units_megabytes(), 2),
49 GB(Bundle.SizeRepresentationUtil_units_gigabytes(), 3),
50 TB(Bundle.SizeRepresentationUtil_units_terabytes(), 4),
51 PB(Bundle.SizeRepresentationUtil_units_petabytes(), 5);
53 private final String suffix;
54 private final long divisor;
61 SizeUnit(String suffix,
int power) {
63 this.divisor = (long) Math.pow(SIZE_CONVERSION_CONSTANT, power);
69 public String getSuffix() {
76 public long getDivisor() {
89 static String getSizeString(Long size) {
90 return getSizeString(size, APPROXIMATE_SIZE_FORMAT,
true);
98 static SizeUnit getSizeUnit(Long size) {
100 return SizeUnit.values()[0];
103 for (SizeUnit unit : SizeUnit.values()) {
104 long result = size / unit.getDivisor();
105 if (result < SIZE_CONVERSION_CONSTANT) {
110 return SizeUnit.values()[SizeUnit.values().length - 1];
124 static String getSizeString(Long size, DecimalFormat format,
boolean showFullSize) {
129 SizeUnit sizeUnit = getSizeUnit(size);
130 if (sizeUnit == null) {
131 sizeUnit = SizeUnit.BYTES;
134 String closestUnitSize = String.format(
"%s %s",
135 format.format(((
double) size) / sizeUnit.getDivisor()), sizeUnit.getSuffix());
137 String fullSize = String.format(
"%d %s", size, SizeUnit.BYTES.getSuffix());
138 if (sizeUnit.equals(SizeUnit.BYTES)) {
140 }
else if (showFullSize) {
141 return String.format(
"%s (%s)", closestUnitSize, fullSize);
143 return closestUnitSize;
152 static DefaultCellModel<?> getBytesCell(Long bytes) {
154 return new DefaultCellModel<>(
"");
156 return new DefaultCellModel<>(bytes, SizeRepresentationUtil::getSizeString);
static final DecimalFormat APPROXIMATE_SIZE_FORMAT
static final int SIZE_CONVERSION_CONSTANT