Enum SizeUnit

    • Enum Constant Detail

      • BYTES

        public static final SizeUnit BYTES
        A byte unit.
      • GIBI_BYTES

        public static final SizeUnit GIBI_BYTES
        A gibi-byte unit.
      • GIGA_BYTES

        public static final SizeUnit GIGA_BYTES
        A giga-byte unit.
      • KIBI_BYTES

        public static final SizeUnit KIBI_BYTES
        A kibi-byte unit.
      • KILO_BYTES

        public static final SizeUnit KILO_BYTES
        A kilo-byte unit.
      • MEBI_BYTES

        public static final SizeUnit MEBI_BYTES
        A mebi-byte unit.
      • MEGA_BYTES

        public static final SizeUnit MEGA_BYTES
        A mega-byte unit.
      • TEBI_BYTES

        public static final SizeUnit TEBI_BYTES
        A tebi-byte unit.
      • TERA_BYTES

        public static final SizeUnit TERA_BYTES
        A tera-byte unit.
    • Method Detail

      • values

        public static SizeUnit[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (SizeUnit c : SizeUnit.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static SizeUnit valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null
      • getBestFitUnit

        public static SizeUnit getBestFitUnit​(long bytes)
        Gets the best-fit unit for the specified number of bytes. The returned unit will be able to represent the number of bytes using a decimal number comprising of an integer part which is greater than zero. Bigger units are chosen in preference to smaller units and binary units are only returned if they are an exact fit. If the number of bytes is zero then the BYTES unit is always returned. For example:
         getBestFitUnit(0)       // BYTES
         getBestFitUnit(999)     // BYTES
         getBestFitUnit(1000)    // KILO_BYTES
         getBestFitUnit(1024)    // KIBI_BYTES
         getBestFitUnit(1025)    // KILO_BYTES
         getBestFitUnit(999999)  // KILO_BYTES
         getBestFitUnit(1000000) // MEGA_BYTES
         
        Parameters:
        bytes - The number of bytes.
        Returns:
        Returns the best fit unit.
        Throws:
        IllegalArgumentException - If bytes is negative.
        See Also:
        getBestFitUnitExact(long)
      • getBestFitUnitExact

        public static SizeUnit getBestFitUnitExact​(long bytes)
        Gets the best-fit unit for the specified number of bytes which can represent the provided value using an integral value. Bigger units are chosen in preference to smaller units. If the number of bytes is zero then the BYTES unit is always returned. For example:
         getBestFitUnitExact(0)       // BYTES
         getBestFitUnitExact(999)     // BYTES
         getBestFitUnitExact(1000)    // KILO_BYTES
         getBestFitUnitExact(1024)    // KIBI_BYTES
         getBestFitUnitExact(1025)    // BYTES
         getBestFitUnitExact(999999)  // BYTES
         getBestFitUnitExact(1000000) // MEGA_BYTES
         
        Parameters:
        bytes - The number of bytes.
        Returns:
        Returns the best fit unit can represent the provided value using an integral value.
        Throws:
        IllegalArgumentException - If bytes is negative.
        See Also:
        getBestFitUnit(long)
      • getUnit

        public static SizeUnit getUnit​(String s)
        Get the unit corresponding to the provided unit name.
        Parameters:
        s - The name of the unit. Can be the abbreviated or long name and can contain white space and mixed case characters.
        Returns:
        Returns the unit corresponding to the provided unit name.
        Throws:
        IllegalArgumentException - If the provided name did not correspond to a known memory size unit.
      • parseValue

        public static long parseValue​(String s)
        Parse the provided size string and return its equivalent size in bytes. The size string must specify the unit e.g. "10kb".
        Parameters:
        s - The size string to be parsed.
        Returns:
        Returns the parsed duration in bytes.
        Throws:
        NumberFormatException - If the provided size string could not be parsed.
      • parseValue

        public static long parseValue​(String s,
                                      SizeUnit defaultUnit)
        Parse the provided size string and return its equivalent size in bytes.
        Parameters:
        s - The size string to be parsed.
        defaultUnit - The default unit to use if there is no unit specified in the size string, or null if the string must always contain a unit.
        Returns:
        Returns the parsed size in bytes.
        Throws:
        NumberFormatException - If the provided size string could not be parsed.
      • fromBytes

        public double fromBytes​(long amount)
        Converts the specified size in bytes to this unit.
        Parameters:
        amount - The size in bytes.
        Returns:
        Returns size in this unit.
      • getLongName

        public String getLongName()
        Get the long name of this unit.
        Returns:
        Returns the long name of this unit.
      • getShortName

        public String getShortName()
        Get the abbreviated name of this unit.
        Returns:
        Returns the abbreviated name of this unit.
      • getSize

        public long getSize()
        Get the number of bytes that this unit represents.
        Returns:
        Returns the number of bytes that this unit represents.
      • toBytes

        public long toBytes​(double amount)
        Converts the specified size in this unit to bytes.
        Parameters:
        amount - The size as a quantity of this unit.
        Returns:
        Returns the number of bytes that the size represents.
        Throws:
        NumberFormatException - If the provided size exceeded long.MAX_VALUE.
      • toString

        public String toString()

        This implementation returns the abbreviated name of this size unit.

        Overrides:
        toString in class Enum<SizeUnit>