Package oshi.util
Class FormatUtil
- java.lang.Object
-
- oshi.util.FormatUtil
-
@ThreadSafe public final class FormatUtil extends java.lang.Object
Formatting utility for appending units or converting between number types.
-
-
Field Summary
Fields Modifier and Type Field Description private static long
EXA
private static long
EXBI
private static long
GIBI
private static long
GIGA
static java.lang.String
HEX_ERROR
ConstantHEX_ERROR="0x%08X"
private static long
KIBI
Binary prefixes, used in IEC Standard for naming bytes.private static long
KILO
Decimal prefixes, used for Hz and other metric units and for bytes by hard drive manufacturersprivate static long
MEBI
private static long
MEGA
private static long
PEBI
private static long
PETA
private static long
TEBI
private static long
TERA
private static java.math.BigInteger
TWOS_COMPLEMENT_REF
-
Constructor Summary
Constructors Modifier Constructor Description private
FormatUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
formatBytes(long bytes)
Format bytes into a rounded string representation using IEC standard (matches Mac/Linux).static java.lang.String
formatBytesDecimal(long bytes)
Format bytes into a rounded string representation using decimal SI units.static java.lang.String
formatElapsedSecs(long secs)
Formats an elapsed time in seconds as days, hh:mm:ss.static java.lang.String
formatError(int errorCode)
Translate an integer error code to its hex notationstatic java.lang.String
formatHertz(long hertz)
Format hertz into a string to a rounded string representation.private static java.lang.String
formatUnits(long value, long prefix, java.lang.String unit)
Format units as exact integer or fractional decimal based on the prefix, appending the appropriate unitsstatic java.lang.String
formatValue(long value, java.lang.String unit)
Format arbitrary units into a string to a rounded string representation.static long
getUnsignedInt(int x)
Convert unsigned int to signed long.static int
roundToInt(double x)
Rounds a floating point number to the nearest integerstatic java.lang.String
toUnsignedString(int i)
Represent a 32 bit value as if it were an unsigned integer.static java.lang.String
toUnsignedString(long l)
Represent a 64 bit value as if it were an unsigned long.
-
-
-
Field Detail
-
KIBI
private static final long KIBI
Binary prefixes, used in IEC Standard for naming bytes. (https://en.wikipedia.org/wiki/International_Electrotechnical_Commission) Should be used for most representations of bytes- See Also:
- Constant Field Values
-
MEBI
private static final long MEBI
- See Also:
- Constant Field Values
-
GIBI
private static final long GIBI
- See Also:
- Constant Field Values
-
TEBI
private static final long TEBI
- See Also:
- Constant Field Values
-
PEBI
private static final long PEBI
- See Also:
- Constant Field Values
-
EXBI
private static final long EXBI
- See Also:
- Constant Field Values
-
KILO
private static final long KILO
Decimal prefixes, used for Hz and other metric units and for bytes by hard drive manufacturers- See Also:
- Constant Field Values
-
MEGA
private static final long MEGA
- See Also:
- Constant Field Values
-
GIGA
private static final long GIGA
- See Also:
- Constant Field Values
-
TERA
private static final long TERA
- See Also:
- Constant Field Values
-
PETA
private static final long PETA
- See Also:
- Constant Field Values
-
EXA
private static final long EXA
- See Also:
- Constant Field Values
-
TWOS_COMPLEMENT_REF
private static final java.math.BigInteger TWOS_COMPLEMENT_REF
-
HEX_ERROR
public static final java.lang.String HEX_ERROR
ConstantHEX_ERROR="0x%08X"
- See Also:
- Constant Field Values
-
-
Method Detail
-
formatBytes
public static java.lang.String formatBytes(long bytes)
Format bytes into a rounded string representation using IEC standard (matches Mac/Linux). For hard drive capacities, use @linkformatBytesDecimal(long)
. For Windows displays for KB, MB and GB, in JEDEC units, edit the returned string to remove the 'i' to display the (incorrect) JEDEC units.- Parameters:
bytes
- Bytes.- Returns:
- Rounded string representation of the byte size.
-
formatUnits
private static java.lang.String formatUnits(long value, long prefix, java.lang.String unit)
Format units as exact integer or fractional decimal based on the prefix, appending the appropriate units- Parameters:
value
- The value to formatprefix
- The divisor of the unit multiplierunit
- A string representing the units- Returns:
- A string with the value
-
formatBytesDecimal
public static java.lang.String formatBytesDecimal(long bytes)
Format bytes into a rounded string representation using decimal SI units. These are used by hard drive manufacturers for capacity. Most other storage should useformatBytes(long)
.- Parameters:
bytes
- Bytes.- Returns:
- Rounded string representation of the byte size.
-
formatHertz
public static java.lang.String formatHertz(long hertz)
Format hertz into a string to a rounded string representation.- Parameters:
hertz
- Hertz.- Returns:
- Rounded string representation of the hertz size.
-
formatValue
public static java.lang.String formatValue(long value, java.lang.String unit)
Format arbitrary units into a string to a rounded string representation.- Parameters:
value
- The valueunit
- Units to append metric prefix to- Returns:
- Rounded string representation of the value with metric prefix to extension
-
formatElapsedSecs
public static java.lang.String formatElapsedSecs(long secs)
Formats an elapsed time in seconds as days, hh:mm:ss.- Parameters:
secs
- Elapsed seconds- Returns:
- A string representation of elapsed time
-
getUnsignedInt
public static long getUnsignedInt(int x)
Convert unsigned int to signed long.- Parameters:
x
- Signed int representing an unsigned integer- Returns:
- long value of x unsigned
-
toUnsignedString
public static java.lang.String toUnsignedString(int i)
Represent a 32 bit value as if it were an unsigned integer. This is a Java 7 implementation of Java 8's Integer.toUnsignedString.- Parameters:
i
- a 32 bit value- Returns:
- the string representation of the unsigned integer
-
toUnsignedString
public static java.lang.String toUnsignedString(long l)
Represent a 64 bit value as if it were an unsigned long. This is a Java 7 implementation of Java 8's Long.toUnsignedString.- Parameters:
l
- a 64 bit value- Returns:
- the string representation of the unsigned long
-
formatError
public static java.lang.String formatError(int errorCode)
Translate an integer error code to its hex notation- Parameters:
errorCode
- The error code- Returns:
- A string representing the error as 0x....
-
roundToInt
public static int roundToInt(double x)
Rounds a floating point number to the nearest integer- Parameters:
x
- the floating point number- Returns:
- the integer
-
-