19 package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
21 import java.text.DecimalFormat;
22 import org.openide.util.NbBundle;
28 final class SizeRepresentationUtil {
30 private static final int SIZE_CONVERSION_CONSTANT = 1000;
31 private static final DecimalFormat APPROXIMATE_SIZE_FORMAT =
new DecimalFormat(
"#.##");
37 "SizeRepresentationUtil_units_bytes=bytes",
38 "SizeRepresentationUtil_units_kilobytes=KB",
39 "SizeRepresentationUtil_units_megabytes=MB",
40 "SizeRepresentationUtil_units_gigabytes=GB",
41 "SizeRepresentationUtil_units_terabytes=TB",
42 "SizeRepresentationUtil_units_petabytes=PB"
45 BYTES(Bundle.SizeRepresentationUtil_units_bytes(),
"#", 0),
46 KB(Bundle.SizeRepresentationUtil_units_kilobytes(),
"#,##0.00,", 1),
47 MB(Bundle.SizeRepresentationUtil_units_megabytes(),
"#,##0.00,,", 2),
48 GB(Bundle.SizeRepresentationUtil_units_gigabytes(),
"#,##0.00,,,", 3),
49 TB(Bundle.SizeRepresentationUtil_units_terabytes(),
"#,##0.00,,,,", 4),
50 PB(Bundle.SizeRepresentationUtil_units_petabytes(),
"#,##0.00,,,,,", 5);
52 private final String suffix;
53 private final String excelFormatString;
54 private final long divisor;
62 SizeUnit(String suffix, String excelFormatString,
int power) {
66 this.excelFormatString = String.format(
"%s \"%s\"", excelFormatString, suffix);
67 this.divisor = (long) Math.pow(SIZE_CONVERSION_CONSTANT, power);
80 String getExcelFormatString() {
81 return excelFormatString;
100 static String getSizeString(Long size) {
101 return getSizeString(size, APPROXIMATE_SIZE_FORMAT,
true);
109 static SizeUnit getSizeUnit(Long size) {
111 return SizeUnit.values()[0];
114 for (SizeUnit unit : SizeUnit.values()) {
115 long result = size / unit.getDivisor();
116 if (result < SIZE_CONVERSION_CONSTANT) {
121 return SizeUnit.values()[SizeUnit.values().length - 1];
135 static String getSizeString(Long size, DecimalFormat format,
boolean showFullSize) {
140 SizeUnit sizeUnit = getSizeUnit(size);
141 if (sizeUnit == null) {
142 sizeUnit = SizeUnit.BYTES;
145 String closestUnitSize = String.format(
"%s %s",
146 format.format(((
double) size) / sizeUnit.getDivisor()), sizeUnit.getSuffix());
148 String fullSize = String.format(
"%d %s", size, SizeUnit.BYTES.getSuffix());
149 if (sizeUnit.equals(SizeUnit.BYTES)) {
151 }
else if (showFullSize) {
152 return String.format(
"%s (%s)", closestUnitSize, fullSize);
154 return closestUnitSize;
163 static DefaultCellModel<?> getBytesCell(Long bytes) {
165 return new DefaultCellModel<>(
"");
167 SizeUnit unit = SizeRepresentationUtil.getSizeUnit(bytes);
169 unit = SizeUnit.BYTES;
172 return new DefaultCellModel<>(bytes, SizeRepresentationUtil::getSizeString, unit.getExcelFormatString());
176 private SizeRepresentationUtil() {