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 | Class and Description |
|---|---|
| private static class | DataSize.DataSizeUtils
Static nested class to support lazy loading of the |
| Modifier and Type | Field and Description |
|---|---|
| private final long | |
| private static final long | BYTES_PER_GB
Bytes per Gigabyte. |
| private static final long | BYTES_PER_KB
Bytes per Kilobyte. |
| private static final long | BYTES_PER_MB
Bytes per Megabyte. |
| private static final long | BYTES_PER_TB
Bytes per Terabyte. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(DataSize
the object to be compared. other)Implements java. |
| public boolean | equals(Object
the reference object with which to compare. obj)Overrides java. |
| public int | |
| public boolean | Returns: true if this size has a size less than zero bytesChecks if this size is negative, excluding zero. |
| public static DataSize | |
| public static DataSize | |
| public static DataSize | Returns: aDataSizethe number of gigabytes, positive or negative gigabytes)Obtain a |
| public static DataSize | Returns: aDataSizethe number of kilobytes, positive or negative kilobytes)Obtain a |
| public static DataSize | Returns: aDataSizethe number of megabytes, positive or negative megabytes)Obtain a |
| public static DataSize | Returns: aDataSizethe number of terabytes, positive or negative terabytes)Obtain a |
| public static DataSize | Returns: the parsedDataSizethe text to parse text)Obtain a |
| public static DataSize | Returns: the parsedDataSizethe text to parse text, DataUnit the default defaultUnit)DataUnit to useObtain a |
| public long | |
| public long | |
| public long | |
| public long | |
| public String | |
| public long |
| bytes | back to summary |
|---|---|
| private final long bytes | |
| BYTES_PER_GB | back to summary |
|---|---|
| private static final long BYTES_PER_GB Bytes per Gigabyte. | |
| BYTES_PER_KB | back to summary |
|---|---|
| private static final long BYTES_PER_KB Bytes per Kilobyte. | |
| BYTES_PER_MB | back to summary |
|---|---|
| private static final long BYTES_PER_MB Bytes per Megabyte. | |
| BYTES_PER_TB | back to summary |
|---|---|
| private static final long BYTES_PER_TB Bytes per Terabyte. | |
| DataSize | back to summary |
|---|---|
| private DataSize(long bytes) | |
| compareTo | back to summary |
|---|---|
| public int compareTo(DataSize other) Implements java. Doc from java. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isNegative | back to summary |
|---|---|
| public boolean isNegative() Checks if this size is negative, excluding zero.
| |
| of | back to summary |
|---|---|
| public static DataSize of(long amount, DataUnit unit) Obtain a
| |
| ofBytes | back to summary |
|---|---|
| public static DataSize ofBytes(long bytes) Obtain a
| |
| ofGigabytes | back to summary |
|---|---|
| public static DataSize ofGigabytes(long gigabytes) Obtain a
| |
| ofKilobytes | back to summary |
|---|---|
| public static DataSize ofKilobytes(long kilobytes) Obtain a
| |
| ofMegabytes | back to summary |
|---|---|
| public static DataSize ofMegabytes(long megabytes) Obtain a
| |
| ofTerabytes | back to summary |
|---|---|
| public static DataSize ofTerabytes(long terabytes) Obtain a
| |
| parse | back to summary |
|---|---|
| public static DataSize parse(CharSequence text) Obtain a Examples: "12KB" -- parses as "12 kilobytes" "5MB" -- parses as "5 megabytes" "20" -- parses as "20 bytes"
| |
| parse | back to summary |
|---|---|
| public static DataSize parse(CharSequence text, DataUnit defaultUnit) Obtain a The string starts with a number followed optionally by a unit matching one of the supported suffixes. If neither a unit nor a default Examples: "12KB" -- parses as "12 kilobytes" "5MB" -- parses as "5 megabytes" "20" -- parses as "20 kilobytes" (where the
| |
| toBytes | back to summary |
|---|---|
| public long toBytes() Return the number of bytes in this instance.
| |
| toGigabytes | back to summary |
|---|---|
| public long toGigabytes() Return the number of gigabytes in this instance.
| |
| toKilobytes | back to summary |
|---|---|
| public long toKilobytes() Return the number of kilobytes in this instance.
| |
| toMegabytes | back to summary |
|---|---|
| public long toMegabytes() Return the number of megabytes in this instance.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| toTerabytes | back to summary |
|---|---|
| public long toTerabytes() Return the number of terabytes in this instance.
| |
PATTERN.
| Modifier and Type | Field and Description |
|---|---|
| private static final Pattern | PATTERN
The pattern for parsing. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static DataUnit |
| PATTERN | back to summary |
|---|---|
| private static final Pattern PATTERN The pattern for parsing. | |
| DataSizeUtils | back to summary |
|---|---|
| private DataSizeUtils() | |
| determineDataUnit | back to summary |
|---|---|
| private static DataUnit determineDataUnit(String suffix, DataUnit defaultUnit) | |