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 2020-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.datasourcesummary.ui;
20 
21 import java.text.DecimalFormat;
22 import org.openide.util.NbBundle;
24 
29 public final class SizeRepresentationUtil {
30 
31  private static final int SIZE_CONVERSION_CONSTANT = 1000;
32  private static final DecimalFormat APPROXIMATE_SIZE_FORMAT = new DecimalFormat("#.##");
33 
37  @NbBundle.Messages({
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"
44  })
45  enum SizeUnit {
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);
52 
53  private final String suffix;
54  private final long divisor;
55 
61  SizeUnit(String suffix, int power) {
62  this.suffix = suffix;
63  this.divisor = (long) Math.pow(SIZE_CONVERSION_CONSTANT, power);
64  }
65 
69  public String getSuffix() {
70  return suffix;
71  }
72 
76  public long getDivisor() {
77  return divisor;
78  }
79  }
80 
89  static String getSizeString(Long size) {
90  return getSizeString(size, APPROXIMATE_SIZE_FORMAT, true);
91  }
92 
98  static SizeUnit getSizeUnit(Long size) {
99  if (size == null) {
100  return SizeUnit.values()[0];
101  }
102 
103  for (SizeUnit unit : SizeUnit.values()) {
104  long result = size / unit.getDivisor();
105  if (result < SIZE_CONVERSION_CONSTANT) {
106  return unit;
107  }
108  }
109 
110  return SizeUnit.values()[SizeUnit.values().length - 1];
111  }
112 
124  static String getSizeString(Long size, DecimalFormat format, boolean showFullSize) {
125  if (size == null) {
126  return "";
127  }
128 
129  SizeUnit sizeUnit = getSizeUnit(size);
130  if (sizeUnit == null) {
131  sizeUnit = SizeUnit.BYTES;
132  }
133 
134  String closestUnitSize = String.format("%s %s",
135  format.format(((double) size) / sizeUnit.getDivisor()), sizeUnit.getSuffix());
136 
137  String fullSize = String.format("%d %s", size, SizeUnit.BYTES.getSuffix());
138  if (sizeUnit.equals(SizeUnit.BYTES)) {
139  return fullSize;
140  } else if (showFullSize) {
141  return String.format("%s (%s)", closestUnitSize, fullSize);
142  } else {
143  return closestUnitSize;
144  }
145  }
146 
152  static DefaultCellModel<?> getBytesCell(Long bytes) {
153  if (bytes == null) {
154  return new DefaultCellModel<>("");
155  } else {
156  return new DefaultCellModel<>(bytes, SizeRepresentationUtil::getSizeString);
157  }
158  }
159 
161  }
162 }

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