Package adams.core

Class ByteFormat

  • All Implemented Interfaces:
    Serializable

    public class ByteFormat
    extends Object
    implements Serializable
    Formatting class for turning byte amounts into kb, mb, etc.

    Format: {b|B}[.N]{k|K|m|M|g|G|t|T|p|P|e|E|z|Z|y|Y[i]}

    • b|B
      "b" outputs the amount without thousand separators, "B" includes them.
    • .N
      Prints "N" decimal places
    • k|K|m|M|g|G|t|T|p|P|e|E|z|Z|y|Y
      Specifies the unit to use: k|K=kilobytes, m|M=megabytes, g|G=gigabytes, t|T=terabytes, p|P=petabytes, e|E=exabytes, z|Z=zettabytes, y|Y=yottabytes
      Lower case does not add a specifier like "KB", upper case does.
    • Adding "i" at the end uses 1024 as base instead of 1000.
    Version:
    $Revision$
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Detail

      • m_Format

        protected String m_Format
        the format string.
      • m_UseThousandSeparators

        protected boolean m_UseThousandSeparators
        whether to use thousand separators or not.
      • m_NumDecimals

        protected int m_NumDecimals
        the number of decimals to use.
      • m_AddUnit

        protected boolean m_AddUnit
        whether to add a unit specifier at the end.
    • Constructor Detail

      • ByteFormat

        public ByteFormat​(String format)
        Initializes the formatter.
        Parameters:
        format - the format to use
    • Method Detail

      • parseFormat

        protected boolean parseFormat​(String format,
                                      boolean justTest)
        Parses the format.
        Parameters:
        format - the format to parse
        justTest - if true then parameters derived won't be set
      • isValid

        public boolean isValid​(String format)
        Tests the format.
        Parameters:
        format - the format to test
        Returns:
        true if valid
      • getFormat

        public String getFormat()
        Returns the format being used.
        Returns:
        the format, null if invalid one provided
      • format

        public String format​(long bytes)
        Formats the given byte amount according to the format.
        Parameters:
        bytes - the amount to format
        Returns:
        the formatted amount
      • format

        public String format​(double bytes)
        Formats the given byte amount according to the format.
        Parameters:
        bytes - the amount to format
        Returns:
        the formatted amount
      • toString

        public String toString()
        Returns a short string description.
        Overrides:
        toString in class Object
        Returns:
        the format string
      • insertThousandSeparators

        public static String insertThousandSeparators​(String number)
        Inserts thousand separators into the number string (integer or float).
        Parameters:
        number - the string to inject with separators
        Returns:
        the processed string
      • toKiloBytes

        public static String toKiloBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into KB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the KB string
      • toMegaBytes

        public static String toMegaBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into MB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the MB string
      • toGigaBytes

        public static String toGigaBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into GB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the GB string
      • toTeraBytes

        public static String toTeraBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into TB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the TB string
      • toPetaBytes

        public static String toPetaBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into PB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the PB string
      • toExaBytes

        public static String toExaBytes​(double bytes,
                                        int decimals)
        Turns the number of bytes into EB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the EB string
      • toZettaBytes

        public static String toZettaBytes​(double bytes,
                                          int decimals)
        Turns the number of bytes into ZB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the ZB string
      • toYottaBytes

        public static String toYottaBytes​(double bytes,
                                          int decimals)
        Turns the number of bytes into YB. Uses 1024 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the YB string
      • toBestFitBytes

        public static String toBestFitBytes​(double bytes,
                                            int decimals)
        Turns the number of bytes in the shortest representation (KB, MB, GB, ...). Uses 1024 as base.
        Parameters:
        bytes - the bytes to convert
        decimals - the number of decimals after the decimal point
        Returns:
        the optimal number string
      • toKibiBytes

        public static String toKibiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into KiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the KiB string
      • toMebiBytes

        public static String toMebiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into MiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the MiB string
      • toGibiBytes

        public static String toGibiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into GiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the GiB string
      • toTebiBytes

        public static String toTebiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into TiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the TiB string
      • toPebiBytes

        public static String toPebiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into PiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the PiB string
      • toExbiBytes

        public static String toExbiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into EiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the EiB string
      • toZebiBytes

        public static String toZebiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into ZiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the ZiB string
      • toYobiBytes

        public static String toYobiBytes​(double bytes,
                                         int decimals)
        Turns the number of bytes into YiB. Uses 1000 as base.
        Parameters:
        bytes - the number of bytes to format
        decimals - the number of decimals after the decimal point
        Returns:
        the YB string
      • toBestFitBiBytes

        public static String toBestFitBiBytes​(double bytes,
                                              int decimals)
        Turns the number of bytes in the shortest representation (KiB, MiB, GiB, ...). Uses 1000 as base.
        Parameters:
        bytes - the bytes to convert
        decimals - the number of decimals after the decimal point
        Returns:
        the optimal number string