Autopsy  4.16.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;
25 
30 public final class SizeRepresentationUtil {
31 
32  private static final int SIZE_CONVERSION_CONSTANT = 1000;
33  private static final DecimalFormat APPROXIMATE_SIZE_FORMAT = new DecimalFormat("#.##");
34 
35  private static List<String> UNITS = Arrays.asList(
36  Bundle.SizeRepresentationUtil_units_bytes(),
37  Bundle.SizeRepresentationUtil_units_kilobytes(),
38  Bundle.SizeRepresentationUtil_units_megabytes(),
39  Bundle.SizeRepresentationUtil_units_gigabytes(),
40  Bundle.SizeRepresentationUtil_units_terabytes(),
41  Bundle.SizeRepresentationUtil_units_petabytes()
42  );
43 
52  public static String getSizeString(Long size) {
53  return getSizeString(size, APPROXIMATE_SIZE_FORMAT, true);
54  }
55 
67  @NbBundle.Messages({
68  "SizeRepresentationUtil_units_bytes= bytes",
69  "SizeRepresentationUtil_units_kilobytes= kB",
70  "SizeRepresentationUtil_units_megabytes= MB",
71  "SizeRepresentationUtil_units_gigabytes= GB",
72  "SizeRepresentationUtil_units_terabytes= TB",
73  "SizeRepresentationUtil_units_petabytes= PB"
74  })
75  public static String getSizeString(Long size, DecimalFormat format, boolean showFullSize) {
76  if (size == null) {
77  return "";
78  }
79  double approximateSize = size;
80  int unitsIndex = 0;
81  for (; unitsIndex < UNITS.size(); unitsIndex++) {
82  if (approximateSize < SIZE_CONVERSION_CONSTANT) {
83  break;
84  } else {
85  approximateSize /= SIZE_CONVERSION_CONSTANT;
86  }
87  }
88 
89  String fullSize = size + UNITS.get(0);
90  String closestUnitSize = format.format(approximateSize) + UNITS.get(unitsIndex);
91 
92  if (unitsIndex == 0) {
93  return fullSize;
94  } else if (showFullSize) {
95  return String.format("%s (%s)", closestUnitSize, fullSize);
96  } else {
97  return closestUnitSize;
98  }
99  }
100 
102  }
103 }
static String getSizeString(Long size, DecimalFormat format, boolean showFullSize)

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