Autopsy  4.19.0
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 2020 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.datasourcesummary.ui;
20 
21 import java.text.DecimalFormat;
22 import java.util.Arrays;
23 import java.util.List;
24 import org.openide.util.NbBundle;
26 
31 public final class SizeRepresentationUtil {
32 
33  private static final int SIZE_CONVERSION_CONSTANT = 1000;
34  private static final DecimalFormat APPROXIMATE_SIZE_FORMAT = new DecimalFormat("#.##");
35 
39  @NbBundle.Messages({
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"
46  })
47  enum SizeUnit {
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);
54 
55  private final String suffix;
56  private final String excelFormatString;
57  private final long divisor;
58 
65  SizeUnit(String suffix, String excelFormatString, int power) {
66  this.suffix = suffix;
67 
68  // based on https://www.mrexcel.com/board/threads/how-do-i-format-cells-to-show-gb-mb-kb.140135/
69  this.excelFormatString = String.format("%s \"%s\"", excelFormatString, suffix);
70  this.divisor = (long) Math.pow(SIZE_CONVERSION_CONSTANT, power);
71  }
72 
76  public String getSuffix() {
77  return suffix;
78  }
79 
83  public String getExcelFormatString() {
84  return excelFormatString;
85  }
86 
90  public long getDivisor() {
91  return divisor;
92  }
93  }
94 
103  static String getSizeString(Long size) {
104  return getSizeString(size, APPROXIMATE_SIZE_FORMAT, true);
105  }
106 
112  static SizeUnit getSizeUnit(Long size) {
113  if (size == null) {
114  return SizeUnit.values()[0];
115  }
116 
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) {
121  return unit;
122  }
123  }
124 
125  return SizeUnit.values()[SizeUnit.values().length - 1];
126  }
127 
139  static String getSizeString(Long size, DecimalFormat format, boolean showFullSize) {
140  if (size == null) {
141  return "";
142  }
143 
144  SizeUnit sizeUnit = getSizeUnit(size);
145  if (sizeUnit == null) {
146  sizeUnit = SizeUnit.BYTES;
147  }
148 
149  String closestUnitSize = String.format("%s %s",
150  format.format(((double) size) / sizeUnit.getDivisor()), sizeUnit.getSuffix());
151 
152  String fullSize = String.format("%d %s", size, SizeUnit.BYTES.getSuffix());
153  if (sizeUnit.equals(SizeUnit.BYTES)) {
154  return fullSize;
155  } else if (showFullSize) {
156  return String.format("%s (%s)", closestUnitSize, fullSize);
157  } else {
158  return closestUnitSize;
159  }
160  }
161 
167  static DefaultCellModel<?> getBytesCell(Long bytes) {
168  if (bytes == null) {
169  return new DefaultCellModel<>("");
170  } else {
171  SizeUnit unit = SizeRepresentationUtil.getSizeUnit(bytes);
172  if (unit == null) {
173  unit = SizeUnit.BYTES;
174  }
175 
176  return new DefaultCellModel<Long>(bytes, SizeRepresentationUtil::getSizeString, unit.getExcelFormatString());
177  }
178  }
179 
181  }
182 }

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.