Package com.saperion.util
Class FileSizeCalcer
java.lang.Object
com.saperion.util.FileSizeCalcer
This class can be statically used to calculate file sizes by given bytes. The devision factors
1000 (KILO_FACTOR) and 1024 (KIBI_FACTOR) are supported.
- Author:
- sts
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
This enumeration defines the devision factors used by the FileSizeCalcer.static enum
This enumeration defines the file size exponents of the CalcFactor to calculate file sizes. -
Method Summary
Modifier and TypeMethodDescriptionstatic long
getBytes
(double value, FileSizeCalcer.CalcFactor factor, FileSizeCalcer.FileSize unit) Gets the size in bytes.static long
getBytesFromKB
(double kb) Converts KB to bytes with a factor of 1024.static long
getBytesFromMB
(double mb) Converts MB to bytes with a factor of 1024.static double
getGB
(long bytes) Calculates the file size in GigaByte of the given bytes.static double
getKB
(long bytes) Calculates the file size in KiloByte of the given bytes.static double
getMB
(long bytes) Calculates the file size in MegaByte of the given bytes.static String
getNiceSize
(long bytes, FileSizeCalcer.CalcFactor calcFactor) Computes the size using the specified factor and automatically adds the proper unit (B, KB or KiB, MB or MiB, GB or GiB, PB or PiB).static double
getPB
(long bytes) Calculates the file size in PetaByte of the given bytes.static double
getSize
(long bytes, FileSizeCalcer.CalcFactor factor, FileSizeCalcer.FileSize size) Returns the size according to the given parameters.static double
getTB
(long bytes) Calculates the file size in TeraByte of the given bytes.
-
Method Details
-
getSize
public static double getSize(long bytes, FileSizeCalcer.CalcFactor factor, FileSizeCalcer.FileSize size) Returns the size according to the given parameters.- Parameters:
bytes
- The number of bytes.factor
- The factor to be used to calculate the size (KIBI_FACTOR(1024) or KILO_FACTOR(1000)).size
- The desired file size.- Returns:
- The calculated file size.
-
getKB
public static double getKB(long bytes) Calculates the file size in KiloByte of the given bytes. Using a factor of 1024.- Parameters:
bytes
- The number of bytes.- Returns:
- The calculated file size.
-
getMB
public static double getMB(long bytes) Calculates the file size in MegaByte of the given bytes. Using a factor of 1024.- Parameters:
bytes
- The number of bytes.- Returns:
- The calculated file size.
-
getGB
public static double getGB(long bytes) Calculates the file size in GigaByte of the given bytes. Using a factor of 1024.- Parameters:
bytes
- The number of bytes.- Returns:
- The calculated file size.
-
getTB
public static double getTB(long bytes) Calculates the file size in TeraByte of the given bytes. Using a factor of 1024.- Parameters:
bytes
- The number of bytes.- Returns:
- The calculated file size.
-
getPB
public static double getPB(long bytes) Calculates the file size in PetaByte of the given bytes. Using a factor of 1024.- Parameters:
bytes
- The number of bytes.- Returns:
- The calculated file size.
-
getBytesFromMB
public static long getBytesFromMB(double mb) Converts MB to bytes with a factor of 1024.- Parameters:
mb
- size in MB- Returns:
- size in bytes
-
getBytesFromKB
public static long getBytesFromKB(double kb) Converts KB to bytes with a factor of 1024.- Parameters:
kb
- size in KB- Returns:
- size in bytes
-
getBytes
public static long getBytes(double value, FileSizeCalcer.CalcFactor factor, FileSizeCalcer.FileSize unit) Gets the size in bytes.- Parameters:
value
- the value of the file sizefactor
- the factor to calculate withunit
- the size unit of the file size- Returns:
- the size in bytes
-
getNiceSize
Computes the size using the specified factor and automatically adds the proper unit (B, KB or KiB, MB or MiB, GB or GiB, PB or PiB). The size is rounded to the next number. Example: 1135 with a factor of 1024 will result in "1 KiB".- Parameters:
bytes
- size in bytescalcFactor
- the factor to use when calculating the size (1024 or 1000)- Returns:
- formatted string with size and unit
-