Class SizeHelper

java.lang.Object
com.helger.io.misc.SizeHelper

@NotThreadSafe public final class SizeHelper extends Object
A utility class that converts sizes into the corresponding Kilobyte, Megabyte etc. notation.
This class is not thread-safe, because the DecimalFormat class is not thread-safe!
Author:
Philip Helger
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Suffix for Bytes
    static final String
    Suffix for Gigabytes
    static final String
    Suffix for Kilobytes
    static final String
    Suffix for Megabytes
    static final String
    Suffix for Petabytes
    static final String
    Suffix for Terabytes
  • Constructor Summary

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    @NonNull String
    getAsGB(long nSize)
    Get the passed size formatted as Gigabytes without fraction digits.
    @NonNull String
    getAsGB(long nSize, int nDecimals)
    Get the passed size formatted as Gigabytes with the specified number of fraction digits.
    @NonNull String
    getAsKB(long nSize)
    Get the passed size formatted as Kilobytes without fraction digits.
    @NonNull String
    getAsKB(long nSize, int nDecimals)
    Get the passed size formatted as Kilobytes with the specified number of fraction digits.
    @NonNull String
    getAsMatching(long nSize)
    Get the best matching formatting of the passed value.
    @NonNull String
    getAsMatching(long nSize, int nDecimals)
    Get the best matching formatting of the passed value.
    @NonNull String
    getAsMatching(@NonNull BigDecimal aSize)
    Get the best matching formatting of the passed value.
    @NonNull String
    getAsMatching(@NonNull BigDecimal aSize, int nDecimals)
    Get the best matching formatting of the passed value.
    @NonNull String
    getAsMatching(@NonNull BigInteger aSize)
    Get the best matching formatting of the passed value.
    @NonNull String
    getAsMatching(@NonNull BigInteger aSize, int nDecimals)
    Get the best matching formatting of the passed value.
    @NonNull String
    getAsMB(long nSize)
    Get the passed size formatted as Megabytes without fraction digits.
    @NonNull String
    getAsMB(long nSize, int nDecimals)
    Get the passed size formatted as Megabytes with the specified number of fraction digits.
    @NonNull String
    getAsPB(long nSize)
    Get the passed size formatted as Petabytes without fraction digits.
    @NonNull String
    getAsPB(long nSize, int nDecimals)
    Get the passed size formatted as Petabytes with the specified number of fraction digits.
    @NonNull String
    getAsTB(long nSize)
    Get the passed size formatted as Terabytes without fraction digits.
    @NonNull String
    getAsTB(long nSize, int nDecimals)
    Get the passed size formatted as Terabytes with the specified number of fraction digits.
    static @NonNull SizeHelper
    getSizeHelperOfLocale(@NonNull Locale aDisplayLocale)
    Get the size helper for the passed locale.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • SizeHelper

      public SizeHelper(@NonNull Locale aDisplayLocale)
      Constructor using a display locale to determine the decimal format symbols.
      Parameters:
      aDisplayLocale - The locale to use for formatting. May not be null.
    • SizeHelper

      public SizeHelper(@NonNull DecimalFormatSymbols aDFS)
      Constructor using explicit decimal format symbols.
      Parameters:
      aDFS - The decimal format symbols to use. May not be null.
  • Method Details

    • getAsKB

      public @NonNull String getAsKB(long nSize)
      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

      public @NonNull String getAsKB(long nSize, @Nonnegative int nDecimals)
      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

      public @NonNull String getAsMB(long nSize)
      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

      public @NonNull String getAsMB(long nSize, @Nonnegative int nDecimals)
      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

      public @NonNull String getAsGB(long nSize)
      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

      public @NonNull String getAsGB(long nSize, @Nonnegative int nDecimals)
      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

      public @NonNull String getAsTB(long nSize)
      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

      public @NonNull String getAsTB(long nSize, @Nonnegative int nDecimals)
      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

      public @NonNull String getAsPB(long nSize)
      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

      public @NonNull String getAsPB(long nSize, @Nonnegative int nDecimals)
      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

      public @NonNull String getAsMatching(@NonNull BigInteger aSize)
      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 be null.
      Returns:
      The string representation
      Throws:
      IllegalArgumentException - If the passed value cannot be fit in a long
    • getAsMatching

      public @NonNull String getAsMatching(@NonNull BigInteger aSize, @Nonnegative int nDecimals)
      Get the best matching formatting of the passed value.
      Parameters:
      aSize - The value to be converted to a size value. May not be null.
      nDecimals - The number of fraction digits.
      Returns:
      The string representation
      Throws:
      IllegalArgumentException - If the passed value cannot be fit in a long
    • getAsMatching

      public @NonNull String getAsMatching(@NonNull BigDecimal aSize)
      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 be null.
      Returns:
      The string representation
      Throws:
      IllegalArgumentException - If the passed value cannot be fit in a long
    • getAsMatching

      public @NonNull String getAsMatching(@NonNull BigDecimal aSize, @Nonnegative int nDecimals)
      Get the best matching formatting of the passed value.
      Parameters:
      aSize - The value to be converted to a size value. May not be null.
      nDecimals - The number of fraction digits.
      Returns:
      The string representation
      Throws:
      IllegalArgumentException - If the passed value cannot be fit in a long
    • getAsMatching

      public @NonNull String getAsMatching(long nSize)
      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

      public @NonNull String getAsMatching(long nSize, @Nonnegative int nDecimals)
      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

      public String toString()
      Overrides:
      toString in class Object
    • getSizeHelperOfLocale

      public static @NonNull SizeHelper getSizeHelperOfLocale(@NonNull Locale aDisplayLocale)
      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-null SizeHelper object for the passed locale.