Class ByteFormatter


  • public class ByteFormatter
    extends Object
    Formats numeric byte values into human-readable strings.
    Author:
    James Murty
    • Constructor Detail

      • ByteFormatter

        public ByteFormatter()
        Constructs a formatter that will use default text to represent byte amounts. Default values used:
        • Gigabyte suffix: " GB"
        • Megabyte suffix: " MB"
        • Kilobyte suffix: " KB"
        • Byte suffix: " B"
        • Precision: 2 decimal places
      • ByteFormatter

        public ByteFormatter​(String gigabyteSuffix,
                             String megabyteSuffix,
                             String kilobyteSuffix,
                             String byteSuffix,
                             int decimalPlaces)
        Constructs a formatter that will use the given values when formatting byte values.
        Parameters:
        gigabyteSuffix - string to display at the end of gigabyte values.
        megabyteSuffix - string to display at the end of megabyte values.
        kilobyteSuffix - string to display at the end of kilobyte values.
        byteSuffix - string to display at the end of byte values.
        decimalPlaces - the number of decimal places to use when converting byte amounts into kilo, mega or giga byte values.
    • Method Detail

      • formatByteSize

        public String formatByteSize​(long byteSize)
        Converts a byte size into a human-readable string, such as "1.43 MB" or "27 KB". The values used are based on powers of 1024, ie 1 KB = 1024 bytes, not 1000 bytes.
        Parameters:
        byteSize - the byte size of some item
        Returns:
        a human-readable description of the byte size