19 package org.sleuthkit.autopsy.datasourcesummary.ui;
21 import java.text.DecimalFormat;
22 import java.util.Arrays;
23 import java.util.List;
24 import org.openide.util.NbBundle;
40 "SizeRepresentationUtil_units_bytes=bytes",
41 "SizeRepresentationUtil_units_kilobytes=KB",
42 "SizeRepresentationUtil_units_megabytes=MB",
43 "SizeRepresentationUtil_units_gigabytes=GB",
44 "SizeRepresentationUtil_units_terabytes=TB",
45 "SizeRepresentationUtil_units_petabytes=PB"
48 BYTES(Bundle.SizeRepresentationUtil_units_bytes(),
"#", 0),
49 KB(Bundle.SizeRepresentationUtil_units_kilobytes(),
"#,##0.00,", 1),
50 MB(Bundle.SizeRepresentationUtil_units_megabytes(),
"#,##0.00,,", 2),
51 GB(Bundle.SizeRepresentationUtil_units_gigabytes(),
"#,##0.00,,,", 3),
52 TB(Bundle.SizeRepresentationUtil_units_terabytes(),
"#,##0.00,,,,", 4),
53 PB(Bundle.SizeRepresentationUtil_units_petabytes(),
"#,##0.00,,,,,", 5);
55 private final String suffix;
56 private final String excelFormatString;
57 private final long divisor;
65 SizeUnit(String suffix, String excelFormatString,
int power) {
69 this.excelFormatString = String.format(
"%s \"%s\"", excelFormatString, suffix);
70 this.divisor = (long) Math.pow(SIZE_CONVERSION_CONSTANT, power);
76 public String getSuffix() {
83 public String getExcelFormatString() {
84 return excelFormatString;
90 public long getDivisor() {
103 static String getSizeString(Long size) {
104 return getSizeString(size, APPROXIMATE_SIZE_FORMAT,
true);
112 static SizeUnit getSizeUnit(Long size) {
114 return SizeUnit.values()[0];
117 for (
int unitsIndex = 0; unitsIndex < SizeUnit.values().length; unitsIndex++) {
118 SizeUnit unit = SizeUnit.values()[unitsIndex];
119 long result = size / unit.getDivisor();
120 if (result < SIZE_CONVERSION_CONSTANT) {
125 return SizeUnit.values()[SizeUnit.values().length - 1];
139 static String getSizeString(Long size, DecimalFormat format,
boolean showFullSize) {
144 SizeUnit sizeUnit = getSizeUnit(size);
145 if (sizeUnit == null) {
146 sizeUnit = SizeUnit.BYTES;
149 String closestUnitSize = String.format(
"%s %s",
150 format.format(((
double) size) / sizeUnit.getDivisor()), sizeUnit.getSuffix());
152 String fullSize = String.format(
"%d %s", size, SizeUnit.BYTES.getSuffix());
153 if (sizeUnit.equals(SizeUnit.BYTES)) {
155 }
else if (showFullSize) {
156 return String.format(
"%s (%s)", closestUnitSize, fullSize);
158 return closestUnitSize;
167 static DefaultCellModel<?> getBytesCell(Long bytes) {
169 return new DefaultCellModel<>(
"");
173 unit = SizeUnit.BYTES;
176 return new DefaultCellModel<Long>(bytes, SizeRepresentationUtil::getSizeString, unit.getExcelFormatString());
static final DecimalFormat APPROXIMATE_SIZE_FORMAT
static final int SIZE_CONVERSION_CONSTANT