Package org.springframework.util.unit
Class DataSize
java.lang.Object
org.springframework.util.unit.DataSize
- All Implemented Interfaces:
- Serializable,- Comparable<DataSize>
A data size, such as '12MB'. 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 | 
- Since:
- 5.1
- Author:
- Stephane Nicoll, Sam Brannen
- See Also:
- 
Method SummaryModifier and TypeMethodDescriptionintbooleaninthashCode()booleanChecks if this size is negative, excluding zero.static DataSizestatic DataSizeofBytes(long bytes) Obtain aDataSizerepresenting the specified number of bytes.static DataSizeofGigabytes(long gigabytes) Obtain aDataSizerepresenting the specified number of gigabytes.static DataSizeofKilobytes(long kilobytes) Obtain aDataSizerepresenting the specified number of kilobytes.static DataSizeofMegabytes(long megabytes) Obtain aDataSizerepresenting the specified number of megabytes.static DataSizeofTerabytes(long terabytes) Obtain aDataSizerepresenting the specified number of terabytes.static DataSizeparse(CharSequence text) static DataSizeparse(CharSequence text, DataUnit defaultUnit) longtoBytes()Return the number of bytes in this instance.longReturn the number of gigabytes in this instance.longReturn the number of kilobytes in this instance.longReturn the number of megabytes in this instance.toString()longReturn the number of terabytes in this instance.
- 
Method Details- 
ofBytesObtain aDataSizerepresenting the specified number of bytes.- Parameters:
- bytes- the number of bytes, positive or negative
- Returns:
- a DataSize
 
- 
ofKilobytesObtain aDataSizerepresenting the specified number of kilobytes.- Parameters:
- kilobytes- the number of kilobytes, positive or negative
- Returns:
- a DataSize
 
- 
ofMegabytesObtain aDataSizerepresenting the specified number of megabytes.- Parameters:
- megabytes- the number of megabytes, positive or negative
- Returns:
- a DataSize
 
- 
ofGigabytesObtain aDataSizerepresenting the specified number of gigabytes.- Parameters:
- gigabytes- the number of gigabytes, positive or negative
- Returns:
- a DataSize
 
- 
ofTerabytesObtain aDataSizerepresenting the specified number of terabytes.- Parameters:
- terabytes- the number of terabytes, positive or negative
- Returns:
- a DataSize
 
- 
of- Parameters:
- amount- the amount of the size, measured in terms of the unit, positive or negative
- Returns:
- a corresponding DataSize
 
- 
parseObtain aDataSizefrom a text string such as"5MB"usingDataUnit.BYTESif no unit is specified.ExamplesText Parsed As Size in Bytes "20" 20 bytes 20 "20B" 20 bytes 20 "12KB" 12 kilobytes 12,288 "5MB" 5 megabytes 5,242,880 Note that the terms and units used in the above examples are based on binary prefixes. Consult the class-level Javadoc for details. - Parameters:
- text- the text to parse
- Returns:
- the parsed DataSize
- See Also:
 
- 
parseObtain aDataSizefrom a text string such as"5MB"using the specified defaultDataUnitif no unit is specified.The string starts with a number followed optionally by a unit matching one of the supported suffixes. If neither a unit nor a default DataUnitis specified,DataUnit.BYTESwill be inferred.ExamplesText Default Unit Parsed As Size in Bytes "20" null20 bytes 20 "20" KILOBYTES20 kilobytes 20,480 "20B" N/A 20 bytes 20 "12KB" N/A 12 kilobytes 12,288 "5MB" N/A 5 megabytes 5,242,880 Note that the terms and units used in the above examples are based on binary prefixes. Consult the class-level Javadoc for details. - Parameters:
- text- the text to parse
- defaultUnit- the default- DataUnitto use
- Returns:
- the parsed DataSize
 
- 
isNegativepublic boolean isNegative()Checks if this size is negative, excluding zero.- Returns:
- true if this size has a size less than zero bytes
 
- 
toBytespublic long toBytes()Return the number of bytes in this instance.- Returns:
- the number of bytes
 
- 
toKilobytespublic long toKilobytes()Return the number of kilobytes in this instance.- Returns:
- the number of kilobytes
 
- 
toMegabytespublic long toMegabytes()Return the number of megabytes in this instance.- Returns:
- the number of megabytes
 
- 
toGigabytespublic long toGigabytes()Return the number of gigabytes in this instance.- Returns:
- the number of gigabytes
 
- 
toTerabytespublic long toTerabytes()Return the number of terabytes in this instance.- Returns:
- the number of terabytes
 
- 
compareTo- Specified by:
- compareToin interface- Comparable<DataSize>
 
- 
toString
- 
equals
- 
hashCodepublic int hashCode()
 
-