DataSize class final

An amount of data, such as an upload limit or a cache size, stored as a whole number of bytes.

It exists so configuration can say 10MB instead of 10485760. The default conversion service turns strings such as 10MB, 512KB or 100 (bytes) and plain ints (bytes) into a DataSize, so an application declares a @ConfigurationProperties field or a @Value parameter of this type and reads the result. Units are powers of 1024.

@ConfigurationProperties('shop.upload')
class UploadProperties {
  // `shop.upload.max-size=10MB`; a plain number would be bytes.
  DataSize maxSize = DataSize.ofMegabytes(5);
}

// Later, in the upload handler:
if (body.length > properties.maxSize.toBytes()) { ... }

Spring (Java):

@ConfigurationProperties("shop.upload")
class UploadProperties {
  private DataSize maxSize = DataSize.ofMegabytes(5);
  // getter and setter
}
Implemented types

Constructors

DataSize.of(int amount, DataUnit unit)
A size of amount times unit, for a unit chosen at run time.
DataSize.ofBytes(int _bytes)
A size of exactly the given number of bytes.
const
DataSize.ofGigabytes(int gigabytes)
A size of gigabytes gigabytes (1024 MB each).
DataSize.ofKilobytes(int kilobytes)
A size of kilobytes kilobytes of 1024 bytes each.
DataSize.ofMegabytes(int megabytes)
A size of megabytes megabytes (1024 KB each).
DataSize.ofTerabytes(int terabytes)
A size of terabytes terabytes (1024 GB each).

Properties

hashCode → int
The hash code for this object.
no setteroverride
isNegative → bool
Whether the size is below zero.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

compareTo(DataSize other) → int
Compares this object to another object.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toBytes() → int
The size in bytes.
toGigabytes() → int
The size in whole gigabytes, rounded toward zero.
toKilobytes() → int
The size in whole kilobytes, rounded toward zero.
toMegabytes() → int
The size in whole megabytes, rounded toward zero.
toString() → String
The size in bytes with a B suffix (10485760B), like Spring's DataSize.toString; parse reads it back.
override
toTerabytes() → int
The size in whole terabytes, rounded toward zero.

Operators

operator ==(Object other) → bool
The equality operator.
override

Static Methods

parse(String text, {DataUnit unit = DataUnit.bytes}) → DataSize
Reads a size written as a whole number with an optional unit: 10MB, 512kb, 100B, 100.