Package alpine.util

Class ByteFormat


  • public class ByteFormat
    extends Object
    Class to render a byte count as a human-readable string such as "1 KB" or "1 GB" instead of a raw number such as "1024" or "1073741824". See format method for examples. Note that the human-readable values are rendered in a rough way (e.g. if it is a little over a KB we call it a KB). Use the format2() methods to display the exact byte count after the human-readable rough count.


    This class does not extend the JDK Format class because the idea was to KISS!
    Since:
    1.0.0
    Author:
    Steve Springett
    • Constructor Detail

      • ByteFormat

        public ByteFormat()
        Construct a ByteFormat() instance with the default names[] array and min/max fraction digits.
        Since:
        1.0.0
    • Method Detail

      • format

        public String format​(int count)
        Given a raw byte count such as 1024 or 1048576, format it in human-readable form such as "1 KB" or "1 GB".


        Here are some example results using the default min/max values for fraction digits:


        0 - 0 bytes 1 - 1 byte 1023 - 1,023 bytes 1024 (1 KB) - 1 KB 1025 - 1 KB 2000 - 2 KB 1048575 - 1,024 KB 1048576 (1 MB) - 1 MB 1048577 - 1 MB 5000000 - 4.8 MB 1073741824 (1 GB) - 1 GB
        Parameters:
        count - int
        Returns:
        String
        Throws:
        NumberFormatException - Thrown if specified count is less than 0.
        Since:
        1.0.0
      • format2

        public String format2​(int count)
        Similar to format(int), but the raw byte count is placed in parentheses following the formatted value (if the value is greater than 1023 bytes). e.g.:


        1023 - 1,023 bytes 1025 - 1 KB (1025 bytes)
        Parameters:
        count - int
        Returns:
        String
        Since:
        1.0.0
      • format

        public String format​(long count)
        Same as format(int), but accepts a long. Note that the maximum unit this class can deal with is gigabytes, so if the specified count is very large it will still be expressed in terms of gigabytes, e.g. "5,000,000 GB".


        Example results:
         format(Integer.MAX_VALUE + 1L)   - 2 GB
         format(Long.MAX_VALUE)           - 8,589,934,592 GB
        Parameters:
        count - int
        Returns:
        String
        Since:
        1.0.0
      • format2

        public String format2​(long count)
        Similar to format(long), but the raw byte count is placed in parentheses following the formatted value (if the value is greater than 1023 bytes). e.g.:
         1023 - 1,023 bytes
         1025 - 1 KB (1025 bytes)
        Parameters:
        count - int
        Returns:
        String
        Since:
        1.0.0
      • setMinimumFractionDigits

        public void setMinimumFractionDigits​(int d)
        Set the minimum number of fraction digits. (See constructor for default.)
        Parameters:
        d - int
        Since:
        1.0.0
      • minimumFractionDigits

        public ByteFormat minimumFractionDigits​(int d)
        Set the minimum number of fraction digits. (See constructor for default.)
        Parameters:
        d - int
        Since:
        1.9.0
      • setMaximumFractionDigits

        public void setMaximumFractionDigits​(int d)
        Set the maximum number of fraction digits. (See constructor for default.)
        Parameters:
        d - int
        Since:
        1.0.0
      • maximumFractionDigits

        public ByteFormat maximumFractionDigits​(int d)
        Set the maximum number of fraction digits. (See constructor for default.)
        Parameters:
        d - int
        Since:
        1.9.0
      • setNames

        public void setNames​(String[] names)
        Set the names[] array to something other than the default one (see constructor). You could do this, for example, if you wanted to spell out "gigabytes" instead of using "GB". Note: The new array must be the same size as the original. (There is no error checking to enforce this.)
        Parameters:
        names - String[]
        Since:
        1.0.0
      • names

        public ByteFormat names​(String[] names)
        Set the names[] array to something other than the default one (see constructor). You could do this, for example, if you wanted to spell out "gigabytes" instead of using "GB". Note: The new array must be the same size as the original. (There is no error checking to enforce this.)
        Parameters:
        names - String[]
        Since:
        1.9.0