Package com.helger.io.misc
Class SizeHelper
java.lang.Object
com.helger.io.misc.SizeHelper
A utility class that converts sizes into the corresponding Kilobyte, Megabyte
etc. notation.
This class is not thread-safe, because the
This class is not thread-safe, because the
DecimalFormat class is not
thread-safe!- Author:
- Philip Helger
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSizeHelper(@NonNull DecimalFormatSymbols aDFS) Constructor using explicit decimal format symbols.SizeHelper(@NonNull Locale aDisplayLocale) Constructor using a display locale to determine the decimal format symbols. -
Method Summary
Modifier and TypeMethodDescription@NonNull StringgetAsGB(long nSize) Get the passed size formatted as Gigabytes without fraction digits.@NonNull StringgetAsGB(long nSize, int nDecimals) Get the passed size formatted as Gigabytes with the specified number of fraction digits.@NonNull StringgetAsKB(long nSize) Get the passed size formatted as Kilobytes without fraction digits.@NonNull StringgetAsKB(long nSize, int nDecimals) Get the passed size formatted as Kilobytes with the specified number of fraction digits.@NonNull StringgetAsMatching(long nSize) Get the best matching formatting of the passed value.@NonNull StringgetAsMatching(long nSize, int nDecimals) Get the best matching formatting of the passed value.@NonNull StringgetAsMatching(@NonNull BigDecimal aSize) Get the best matching formatting of the passed value.@NonNull StringgetAsMatching(@NonNull BigDecimal aSize, int nDecimals) Get the best matching formatting of the passed value.@NonNull StringgetAsMatching(@NonNull BigInteger aSize) Get the best matching formatting of the passed value.@NonNull StringgetAsMatching(@NonNull BigInteger aSize, int nDecimals) Get the best matching formatting of the passed value.@NonNull StringgetAsMB(long nSize) Get the passed size formatted as Megabytes without fraction digits.@NonNull StringgetAsMB(long nSize, int nDecimals) Get the passed size formatted as Megabytes with the specified number of fraction digits.@NonNull StringgetAsPB(long nSize) Get the passed size formatted as Petabytes without fraction digits.@NonNull StringgetAsPB(long nSize, int nDecimals) Get the passed size formatted as Petabytes with the specified number of fraction digits.@NonNull StringgetAsTB(long nSize) Get the passed size formatted as Terabytes without fraction digits.@NonNull StringgetAsTB(long nSize, int nDecimals) Get the passed size formatted as Terabytes with the specified number of fraction digits.static @NonNull SizeHelpergetSizeHelperOfLocale(@NonNull Locale aDisplayLocale) Get the size helper for the passed locale.toString()
-
Field Details
-
B_SUFFIX
Suffix for Bytes- See Also:
-
KB_SUFFIX
Suffix for Kilobytes- See Also:
-
MB_SUFFIX
Suffix for Megabytes- See Also:
-
GB_SUFFIX
Suffix for Gigabytes- See Also:
-
TB_SUFFIX
Suffix for Terabytes- See Also:
-
PB_SUFFIX
Suffix for Petabytes- See Also:
-
-
Constructor Details
-
SizeHelper
Constructor using a display locale to determine the decimal format symbols.- Parameters:
aDisplayLocale- The locale to use for formatting. May not benull.
-
SizeHelper
Constructor using explicit decimal format symbols.- Parameters:
aDFS- The decimal format symbols to use. May not benull.
-
-
Method Details
-
getAsKB
Get the passed size formatted as Kilobytes without fraction digits.- Parameters:
nSize- The size in bytes.- Returns:
- The formatted string with the KB suffix. Never
null.
-
getAsKB
Get the passed size formatted as Kilobytes with the specified number of fraction digits.- Parameters:
nSize- The size in bytes.nDecimals- The number of fraction digits to use. Must be ≥ 0.- Returns:
- The formatted string with the KB suffix. Never
null.
-
getAsMB
Get the passed size formatted as Megabytes without fraction digits.- Parameters:
nSize- The size in bytes.- Returns:
- The formatted string with the MB suffix. Never
null.
-
getAsMB
Get the passed size formatted as Megabytes with the specified number of fraction digits.- Parameters:
nSize- The size in bytes.nDecimals- The number of fraction digits to use. Must be ≥ 0.- Returns:
- The formatted string with the MB suffix. Never
null.
-
getAsGB
Get the passed size formatted as Gigabytes without fraction digits.- Parameters:
nSize- The size in bytes.- Returns:
- The formatted string with the GB suffix. Never
null.
-
getAsGB
Get the passed size formatted as Gigabytes with the specified number of fraction digits.- Parameters:
nSize- The size in bytes.nDecimals- The number of fraction digits to use. Must be ≥ 0.- Returns:
- The formatted string with the GB suffix. Never
null.
-
getAsTB
Get the passed size formatted as Terabytes without fraction digits.- Parameters:
nSize- The size in bytes.- Returns:
- The formatted string with the TB suffix. Never
null.
-
getAsTB
Get the passed size formatted as Terabytes with the specified number of fraction digits.- Parameters:
nSize- The size in bytes.nDecimals- The number of fraction digits to use. Must be ≥ 0.- Returns:
- The formatted string with the TB suffix. Never
null.
-
getAsPB
Get the passed size formatted as Petabytes without fraction digits.- Parameters:
nSize- The size in bytes.- Returns:
- The formatted string with the PB suffix. Never
null.
-
getAsPB
Get the passed size formatted as Petabytes with the specified number of fraction digits.- Parameters:
nSize- The size in bytes.nDecimals- The number of fraction digits to use. Must be ≥ 0.- Returns:
- The formatted string with the PB suffix. Never
null.
-
getAsMatching
Get the best matching formatting of the passed value. No fraction digits will be emitted.- Parameters:
aSize- The value to be converted to a size value. May not benull.- Returns:
- The string representation
- Throws:
IllegalArgumentException- If the passed value cannot be fit in a long
-
getAsMatching
Get the best matching formatting of the passed value.- Parameters:
aSize- The value to be converted to a size value. May not benull.nDecimals- The number of fraction digits.- Returns:
- The string representation
- Throws:
IllegalArgumentException- If the passed value cannot be fit in a long
-
getAsMatching
Get the best matching formatting of the passed value. No fraction digits will be emitted.- Parameters:
aSize- The value to be converted to a size value. May not benull.- Returns:
- The string representation
- Throws:
IllegalArgumentException- If the passed value cannot be fit in a long
-
getAsMatching
Get the best matching formatting of the passed value.- Parameters:
aSize- The value to be converted to a size value. May not benull.nDecimals- The number of fraction digits.- Returns:
- The string representation
- Throws:
IllegalArgumentException- If the passed value cannot be fit in a long
-
getAsMatching
Get the best matching formatting of the passed value. No fraction digits will be emitted.- Parameters:
nSize- The size in bytes.- Returns:
- The string representation using the best matching unit. Never
null.
-
getAsMatching
Get the best matching formatting of the passed value.- Parameters:
nSize- The size in bytes.nDecimals- The number of fraction digits to use. Must be ≥ 0.- Returns:
- The string representation using the best matching unit. Never
null.
-
toString
-
getSizeHelperOfLocale
Get the size helper for the passed locale. The locale determines the formatting of the numeric value.- Parameters:
aDisplayLocale- The locale to be used.- Returns:
- The non-
nullSizeHelperobject for the passed locale.
-