Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
SizeRepresentationUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
20 
21 import java.text.DecimalFormat;
22 import org.openide.util.NbBundle;
23 
28 final class SizeRepresentationUtil {
29 
30  private static final int SIZE_CONVERSION_CONSTANT = 1000;
31  private static final DecimalFormat APPROXIMATE_SIZE_FORMAT = new DecimalFormat("#.##");
32 
36  @NbBundle.Messages({
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"
43  })
44  enum SizeUnit {
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);
51 
52  private final String suffix;
53  private final String excelFormatString;
54  private final long divisor;
55 
62  SizeUnit(String suffix, String excelFormatString, int power) {
63  this.suffix = suffix;
64 
65  // based on https://www.mrexcel.com/board/threads/how-do-i-format-cells-to-show-gb-mb-kb.140135/
66  this.excelFormatString = String.format("%s \"%s\"", excelFormatString, suffix);
67  this.divisor = (long) Math.pow(SIZE_CONVERSION_CONSTANT, power);
68  }
69 
73  String getSuffix() {
74  return suffix;
75  }
76 
80  String getExcelFormatString() {
81  return excelFormatString;
82  }
83 
87  long getDivisor() {
88  return divisor;
89  }
90  }
91 
100  static String getSizeString(Long size) {
101  return getSizeString(size, APPROXIMATE_SIZE_FORMAT, true);
102  }
103 
109  static SizeUnit getSizeUnit(Long size) {
110  if (size == null) {
111  return SizeUnit.values()[0];
112  }
113 
114  for (SizeUnit unit : SizeUnit.values()) {
115  long result = size / unit.getDivisor();
116  if (result < SIZE_CONVERSION_CONSTANT) {
117  return unit;
118  }
119  }
120 
121  return SizeUnit.values()[SizeUnit.values().length - 1];
122  }
123 
135  static String getSizeString(Long size, DecimalFormat format, boolean showFullSize) {
136  if (size == null) {
137  return "";
138  }
139 
140  SizeUnit sizeUnit = getSizeUnit(size);
141  if (sizeUnit == null) {
142  sizeUnit = SizeUnit.BYTES;
143  }
144 
145  String closestUnitSize = String.format("%s %s",
146  format.format(((double) size) / sizeUnit.getDivisor()), sizeUnit.getSuffix());
147 
148  String fullSize = String.format("%d %s", size, SizeUnit.BYTES.getSuffix());
149  if (sizeUnit.equals(SizeUnit.BYTES)) {
150  return fullSize;
151  } else if (showFullSize) {
152  return String.format("%s (%s)", closestUnitSize, fullSize);
153  } else {
154  return closestUnitSize;
155  }
156  }
157 
163  static DefaultCellModel<?> getBytesCell(Long bytes) {
164  if (bytes == null) {
165  return new DefaultCellModel<>("");
166  } else {
167  SizeUnit unit = SizeRepresentationUtil.getSizeUnit(bytes);
168  if (unit == null) {
169  unit = SizeUnit.BYTES;
170  }
171 
172  return new DefaultCellModel<>(bytes, SizeRepresentationUtil::getSizeString, unit.getExcelFormatString());
173  }
174  }
175 
176  private SizeRepresentationUtil() {
177  }
178 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Oct 4 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.