public final class DataSize extends Object implements Comparable<DataSize>
This class models data size in terms of bytes and is immutable and thread-safe.
The terms and units used in this class are based on
binary prefixes
indicating multiplication by powers of 2. Consult the following table and
the Javadoc for DataUnit
for details.
Term | Data Size | Size in Bytes |
---|---|---|
byte | 1B | 1 |
kilobyte | 1KB | 1,024 |
megabyte | 1MB | 1,048,576 |
gigabyte | 1GB | 1,073,741,824 |
terabyte | 1TB | 1,099,511,627,776 |
DataUnit
Modifier and Type | Method and Description |
---|---|
int |
compareTo(DataSize other) |
boolean |
equals(Object other) |
int |
hashCode() |
boolean |
isNegative()
Checks if this size is negative, excluding zero.
|
static DataSize |
of(long amount,
DataUnit unit)
|
static DataSize |
ofBytes(long bytes)
Obtain a
DataSize representing the specified number of bytes. |
static DataSize |
ofGigabytes(long gigabytes)
Obtain a
DataSize representing the specified number of gigabytes. |
static DataSize |
ofKilobytes(long kilobytes)
Obtain a
DataSize representing the specified number of kilobytes. |
static DataSize |
ofMegabytes(long megabytes)
Obtain a
DataSize representing the specified number of megabytes. |
static DataSize |
ofTerabytes(long terabytes)
Obtain a
DataSize representing the specified number of terabytes. |
static DataSize |
parse(CharSequence text)
|
static DataSize |
parse(CharSequence text,
DataUnit defaultUnit)
|
long |
toBytes()
Return the number of bytes in this instance.
|
long |
toGigabytes()
Return the number of gigabytes in this instance.
|
long |
toKilobytes()
Return the number of kilobytes in this instance.
|
long |
toMegabytes()
Return the number of megabytes in this instance.
|
String |
toString() |
long |
toTerabytes()
Return the number of terabytes in this instance.
|
public static DataSize ofBytes(long bytes)
DataSize
representing the specified number of bytes.bytes
- the number of bytes, positive or negativeDataSize
public static DataSize ofKilobytes(long kilobytes)
DataSize
representing the specified number of kilobytes.kilobytes
- the number of kilobytes, positive or negativeDataSize
public static DataSize ofMegabytes(long megabytes)
DataSize
representing the specified number of megabytes.megabytes
- the number of megabytes, positive or negativeDataSize
public static DataSize ofGigabytes(long gigabytes)
DataSize
representing the specified number of gigabytes.gigabytes
- the number of gigabytes, positive or negativeDataSize
public static DataSize ofTerabytes(long terabytes)
DataSize
representing the specified number of terabytes.terabytes
- the number of terabytes, positive or negativeDataSize
public static DataSize of(long amount, DataUnit unit)
amount
- the amount of the size, measured in terms of the unit,
positive or negativeDataSize
public static DataSize parse(CharSequence text)
DataSize
from a text string such as 12MB
using
DataUnit.BYTES
if no unit is specified.
Examples:
"12KB" -- parses as "12 kilobytes" "5MB" -- parses as "5 megabytes" "20" -- parses as "20 bytes"
text
- the text to parseDataSize
parse(CharSequence, DataUnit)
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit)
DataSize
from a text string such as 12MB
using
the specified default DataUnit
if no unit is specified.
The string starts with a number followed optionally by a unit matching one of the supported suffixes.
Examples:
"12KB" -- parses as "12 kilobytes" "5MB" -- parses as "5 megabytes" "20" -- parses as "20 kilobytes" (where thedefaultUnit
isDataUnit.KILOBYTES
)
text
- the text to parseDataSize
public boolean isNegative()
public long toBytes()
public long toKilobytes()
public long toMegabytes()
public long toGigabytes()
public long toTerabytes()
public int compareTo(DataSize other)
compareTo
in interface Comparable<DataSize>