A small utility library for working with units of digital information, featuring:
- Conversion
- Comparison
- Parsing
- Formatting
Built for Java 8+.
The DataSize
class is the main focus of the library. It's an immutable container of a number and a unit.
DataSize fromDouble = DataSize.of(Math.PI, ByteUnit.SI.GIBIBYTE);
DataSize fromBigDecimal = DataSize.of(BigDecimal.valueOf(Math.PI), ByteUnit.SI.GIBIBYTE);
DataSize fromString = DataSize.parse("3.14 GiB");
It features simple & convenient methods for conversion, formatting and parsing:
// Conversion
DataSize withNaturalUnit = DataSize.of(1024, ByteUnit.BYTE).toNaturalUnit();
assertEquals(withNaturalUnit.getUnit(), ByteUnit.IEC.KIBIBYTE);
DataSize dataSize = DataSize.of(1024, ByteUnit.BYTE).toUnit(ByteUnit.SI.KILOBYTE);
assertEquals(dataSize.getValue(), BigDecimal.valueOf(1.024));
// Formatting
DataSize dataSize = DataSize.of(1000, ByteUnit.BYTE);
String formatted = dataSize.toString();
assertEquals(formatted, "1 kB");
// Parsing
DataSize oneByte = DataSize.parse("1 B");
DataSize twoByte = DataSize.parse("2B");
See the JavaDoc for each class for detailed usage.
Using Gradle, add this to your build script:
repositories {
mavenCentral()
}
dependencies {
compile 'io.aesy:datasize:1.0.0'
}
Using Maven, add this to your list of dependencies in pom.xml
:
<dependency>
<groupId>io.aesy</groupId>
<artifactId>datasize</artifactId>
<version>1.0.0</version>
</dependency>
Use the issue tracker to report bugs or make feature requests. Pull requests are welcome, but it may be a good idea to create an issue to discuss any changes beforehand.
MIT, see LICENSE file.