1 = Storage-Units image:https://img.shields.io/badge/email-%40metio-brightgreen.svg?style=social&label=mail["Discuss on Google Groups", link="https://groups.google.com/forum/#!forum/metio"] image:https://img.shields.io/badge/irc-%23metio.wtf-brightgreen.svg?style=social&label=IRC["Chat on IRC", link="http://webchat.freenode.net/?channels=metio.wtf"]
2 Sebastian Hoß <http://seb.xn--ho-hia.de/[@sebhoss]>
4 :project-name: storage-units
5 :project-group: de.xn--ho-hia.storage_units
6 :coverity-project: 2658
7 :codacy-project: d3cfbbc415c14b79a661d573ac11e68c
9 :toc-placement: preamble
11 image:https://img.shields.io/badge/license-cc%20zero-000000.svg?style=flat-square["CC Zero", link="http://creativecommons.org/publicdomain/zero/1.0/"]
12 pass:[<span class="image"><a class="image" href="https://maven-badges.herokuapp.com/maven-central/de.xn--ho-hia.storage_units/storage-units"><img src="https://img.shields.io/maven-central/v/de.xn--ho-hia.storage_units/storage-units.svg?style=flat-square" alt="Maven Central"></a></span>]
13 pass:[<span class="image"><a class="image" href="https://www.javadoc.io/doc/de.xn--ho-hia.storage_units/storage-units"><img src="https://www.javadoc.io/badge/de.xn--ho-hia.storage_units/storage-units.svg?style=flat-square&color=blue" alt="Read JavaDocs"></a></span>]
14 image:https://reposs.herokuapp.com/?path={github-org}/{project-name}&style=flat-square["Repository size"]
15 image:https://www.openhub.net/p/{project-name}/widgets/project_thin_badge.gif["Open Hub statistics", link="https://www.openhub.net/p/{project-name}"]
17 image:https://img.shields.io/travis/{github-org}/{project-name}/master.svg?style=flat-square["Build Status", link="https://travis-ci.org/{github-org}/{project-name}"]
18 image:https://img.shields.io/coveralls/{github-org}/{project-name}/master.svg?style=flat-square["Code Coverage", link="https://coveralls.io/github/{github-org}/{project-name}"]
19 image:https://img.shields.io/coverity/scan/{coverity-project}.svg?style=flat-square["Coverity Scan Result", link="https://scan.coverity.com/projects/{github-org}-{project-name}"]
20 image:https://img.shields.io/codacy/grade/{codacy-project}.svg?style=flat-square["Codacy Code Quality", link="https://www.codacy.com/app/mail_7/{project-name}"]
21 image:https://img.shields.io/badge/forkable-yes-brightgreen.svg?style=flat-square["Can this project be forked?", link="https://basicallydan.github.io/forkability/?u={github-org}&r={project-name}"]
22 image:https://img.shields.io/maintenance/yes/2016.svg?style=flat-square["Is this thing still maintained?"]
23 image:https://img.shields.io/bountysource/team/metio/activity.svg?style=flat-square["Bounties on open tickets", link="https://www.bountysource.com/teams/metio"]
25 https://www.java.com[Java] library for storage-/byte-units. All units defined in link:http://en.wikipedia.org/wiki/ISO/IEC_80000[ISO IEC 80000-13:2008] are supported, as well as other commonly found units, like 1 Kilobyte = 1024 Byte.
29 * Immutable, type- and thread-safe object model for storage units
30 * Convenience factories to create units
31 * Basic arithmetic operators
32 * Comparisons and equality checks between units
33 * Lossless conversion between all units
34 * Human readable text format, including custom formats
35 * Compatible with any `java.lang.Number`
36 * Custom serializers for Jackson, MongoDB & EclipseLink
42 | Name | Symbol | Exponential | Absolute | Class
71 | 1 099 511 627 776 Byte
77 | 1 125 899 906 842 624 Byte
83 | 1 152 921 504 606 846 976 Byte
89 | 1 180 591 620 717 411 303 424 Byte
95 | 1 208 925 819 614 629 174 706 176 Byte
101 | Name | Symbol | Exponential | Absolute | Class
130 | 1 000 000 000 000 Byte
136 | 1 000 000 000 000 000 Byte
142 | 1 000 000 000 000 000 000 Byte
148 | 1 000 000 000 000 000 000 000 Byte
154 | 1 000 000 000 000 000 000 000 000 Byte
160 | Name | Symbol | Exponential | Absolute | Class
189 | 1 099 511 627 776 Byte
195 | 1 125 899 906 842 624 Byte
201 | 1 152 921 504 606 846 976 Byte
207 | 1 180 591 620 717 411 303 424 Byte
213 | 1 208 925 819 614 629 174 706 176 Byte
217 === Development Status
219 All units are implemented. For future ideas, take a look at the link:https://github.com/sebhoss/storage-units/issues[open tickets] in case you are interested.
226 Each unit implements a Byte-based static factory method (`valueOf(BigInteger)` or `valueOf(long)`) that can be used to represent a given number of bytes in a specific unit. Note that `Long.MAX_VALUE == 8 Exabyte`, thus use `BigInteger` if you want to work with anything bigger than a eight Exabyte. When in doubt, always use `BigInteger`.
231 Kilobyte unit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"
232 Kibibyte unit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"
233 CommonKilobyte unit = CommonKilobyte.valueOf(512) // 512 Byte or "0.50 kB"
235 Megabyte unit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"
236 Mebibyte unit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"
237 CommonMegabyte unit = CommonMegabyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MB"
239 // 'BigInteger' based
240 Kilobyte unit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"
241 Kibibyte unit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"
242 CommonKilobyte unit = CommonKilobyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KB"
244 Megabyte unit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"
245 Mebibyte unit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"
246 CommonMegabyte unit = CommonMegabyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"
249 The `StorageUnits` class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.
256 StorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)
257 StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)
259 // 'BigInteger' based
260 StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)
261 StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)
269 StorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)
270 StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)
272 // 'BigInteger' based
273 StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)
274 StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)
282 StorageUnit<?> unit = StorageUnits.commonValueOf(256) // CommonKilobyte (0.25 kB)
283 StorageUnit<?> unit = StorageUnits.commonValueOf(1048576) // CommonMebibyte (1.00 MB)
285 // 'BigInteger' based
286 StorageUnit<?> unit = StorageUnits.commonValueOf(BigInteger.valueOf(256)) // CommonKilobyte (0.25 kB)
287 StorageUnit<?> unit = StorageUnits.commonValueOf(BigInteger.valueOf(1048576)) // CommonMebibyte (1.00 MB)
290 Additionally high-level factory methods are also available in the `StorageUnits` class.
294 import static de.xn__ho_hia.storage_unit.StorageUnits.*;
296 Kibibyte unit = kibibyte(1) // 1 024 Byte
297 Mebibyte unit = mebibyte(1) // 1 048 576 Byte
298 Gibibyte unit = gibibyte(1) // 1 073 741 824 Byte
299 Tebibyte unit = tebibyte(1) // 1 099 511 627 776 Byte
300 Pebibyte unit = pebibyte(1) // 1 125 899 906 842 624 Byte
301 Exbibyte unit = exbibyte(1) // 1 152 921 504 606 846 976 Byte
302 Zebibyte unit = zebibyte(1) // 1 180 591 620 717 411 303 424 Byte
303 Yobibyte unit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 Byte
305 Kilobyte unit = kilobyte(1) // 1 000 Byte
306 Megabyte unit = megabyte(1) // 1 000 000 Byte
307 Gigabyte unit = gigabyte(1) // 1 000 000 000 Byte
308 Terabyte unit = terabyte(1) // 1 000 000 000 000 Byte
309 Petabyte unit = petabyte(1) // 1 000 000 000 000 000 Byte
310 Exabyte unit = exabyte(1) // 1 000 000 000 000 000 000 Byte
311 Zettabyte unit = zettabyte(1) // 1 000 000 000 000 000 000 000 Byte
312 Yottabyte unit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 Byte
314 CommonKilobyte unit = commonKilobyte(1) // 1 024 Byte
315 CommonMegabyte unit = commonMegabyte(1) // 1 048 576 Byte
316 CommonGigabyte unit = commonGigabyte(1) // 1 073 741 824 Byte
317 CommonTerabyte unit = commonTerabyte(1) // 1 099 511 627 776 Byte
318 CommonPetabyte unit = commonPetabyte(1) // 1 125 899 906 842 624 Byte
319 CommonExabyte unit = commonExabyte(1) // 1 152 921 504 606 846 976 Byte
320 CommonZettabyte unit = commonZettabyte(1) // 1 180 591 620 717 411 303 424 Byte
321 CommonYottabyte unit = commonYottabyte(1) // 1 208 925 819 614 629 174 706 176 Byte
324 === Add, Subtract, Multiply, Divide
326 Each unit implements the basic four math operations. All operations retain their original type, e.g. `[Kilobyte] + [Megabyte] = [Kilobyte]`
330 import static de.xn__ho_hia.storage_unit.StorageUnits.*;
332 kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Byte
333 kibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Byte
334 kibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Byte
335 megabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Byte
336 gigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyte
337 terabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte
340 === Comparison & Equality
342 Each unit is comparable to each other unit.
346 import static de.xn__ho_hia.storage_unit.StorageUnits.*;
348 kibibyte(1024).compareTo(mebibyte(1)) == 0 // true
349 kibibyte(1000).compareTo(mebibyte(1)) == 0 // false
350 petabyte(3).compareTo(terabyte(3000)) == 0 // true
352 megabyte(1000).equals(gigabyte(1)) // true
353 megabyte(1024).equals(gigabyte(1)) // false
354 terabyte(12).equals(gigabyte(12000)) // true
359 Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.
363 import static de.xn__ho_hia.storage_unit.StorageUnits.*;
365 // default pattern '0.00'
366 terabyte(2).toString() // "2.00 TB"
367 gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"
368 petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"
370 // use custom pattern
371 kilobyte(212345).toString("0.0") // "212345.0 kB"
372 gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"
373 kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"
375 // use custom pattern with specific Locale
376 kilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"
377 gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"
380 Format customFormat = new DecimalFormat("#.00000");
381 terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"
383 // without creating unit type first
384 long numberOfBytes = 1_000_000_000_000_000L;
385 formatAsPetabyte(numberOfBytes) // "1.00 PB"
386 formatAsTerabyte(numberOfBytes) // "1000.00 TB"
387 formatAsPebibyte(numberOfBytes) // "0.89 PiB"
389 // use custom pattern
390 formatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"
391 formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"
393 // use custom pattern with specific Locale
394 formatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"
395 formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"
398 formatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"
399 formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"
404 Each unit can be converted to each other unit without loss of information.
408 import static de.xn__ho_hia.storage_unit.StorageUnits.*;
410 Megabyte unit = kilobyte(1000).asMegabyte() // "1.00 MB"
411 Kilobyte unit = gigabyte(12).asKilobyte() // "12000000.00 kB"
412 Gigabyte unit = terabyte(1).asGigabyte() // "1000.00 GB"
414 // convert to best-match
415 kilobyte(1100).asBestMatchingUnit() // "1.10 MB"
416 kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"
417 kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"
418 kilobyte(1100).asBestMatchingCommonUnit() // "1.05 MB"
421 Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)
425 import static de.xn__ho_hia.storage_unit.StorageUnits.*;
427 BigDecimal kilobytes = megabyte(1).inKilobyte() // 1 000
428 BigInteger bytes = kibibyte(2).inByte() // 2 048
429 BigDecimal terabytes = gigabyte(15).inTerabyte() // 0.015
434 The storage-units-(eclipselink|mongodb|jackson) modules provide custom serializers to store storage units.
438 Use any of the three converters like this:
443 public class HardDisk implements Serializable {
447 name="binaryConverter",
448 converterClass=de.xn__ho_hia.storage_unit.eclipselink.BinaryStorageUnitConverter.class
450 @Convert("binaryConverter")
451 public StorageUnit<?> getFreeSize() {
457 name="commonConverter",
458 converterClass=de.xn__ho_hia.storage_unit.eclipselink.CommonStorageUnitConverter.class
460 @Convert("commonConverter")
461 public StorageUnit<?> getUsedSize() {
467 name="decimalConverter",
468 converterClass=de.xn__ho_hia.storage_unit.eclipselink.DecimalyStorageUnitConverter.class
470 @Convert("decimalConverter")
471 public StorageUnit<?> getTotalSize() {
480 Use any of the three codecs like this:
484 CodecRegistry binaryRegistry = CodecRegistries.fromCodecs(new BinaryStorageUnitCodec(), ...);
485 CodecRegistry commonRegistry = CodecRegistries.fromCodecs(new CommonStorageUnitCodec(), ...);
486 CodecRegistry decimalRegistry = CodecRegistries.fromCodecs(new DecimalStorageUnitCodec(), ...);
491 Use the provided `StorageUnitModule` like this:
495 ObjectMapper objectMapper = new ObjectMapper();
496 objectMapper.registerModule(new StorageUnitModule()); // defaults to binary units
497 objectMapper.registerModule(new StorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
498 objectMapper.registerModule(new StorageUnitModule(StorageUnitModule.PreferredUnitType.COMMON));
499 objectMapper.registerModule(new StorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));
504 To use this project just declare the following dependency inside your POM:
506 [source,xml,subs="attributes,verbatim"]
510 <groupId>{project-group}</groupId>
511 <artifactId>storage-units</artifactId>
512 <version>${version.storage-units}</version>
515 <!-- EclipseLink ONLY -->
517 <groupId>{project-group}</groupId>
518 <artifactId>storage-units-eclipselink</artifactId>
519 <version>${version.storage-units}</version>
521 <!-- EclipseLink ONLY -->
523 <!-- MongoDB ONLY -->
525 <groupId>{project-group}</groupId>
526 <artifactId>storage-units-mongodb</artifactId>
527 <version>${version.storage-units}</version>
529 <!-- MongoDB ONLY -->
531 <!-- Jackson ONLY -->
533 <groupId>{project-group}</groupId>
534 <artifactId>storage-units-jackson</artifactId>
535 <version>${version.storage-units}</version>
537 <!-- Jackson ONLY -->
541 Replace `${version.storage-units}` with the link:++http://search.maven.org/#search%7Cga%7C1%7Cg%3Ade.xn--ho-hia.storage_units++[latest release]. This project follows the link:http://semver.org/[semantic versioning guidelines].
545 This project is compatible with the following Java versions:
549 | | 1.X.Y | 2.X.Y | 3.X.Y | 4.X.Y
566 Originally inspired by link:https://github.com/twitter/util#space[Twitters util] package.
570 * link:https://github.com/JakeWharton/byteunits[Byte Units]
571 * link:https://github.com/trivago/triava[triava]
575 To the extent possible under law, the author(s) have dedicated all copyright
576 and related and neighboring rights to this software to the public domain
577 worldwide. This software is distributed without any warranty.
579 You should have received a copy of the CC0 Public Domain Dedication along
580 with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/.
584 * https://github.com/sebhoss/{project-name}
585 * https://bitbucket.org/sebhoss/{project-name}
586 * https://gitlab.com/sebastian.hoss/{project-name}
587 * http://v2.pikacode.com/sebhoss/{project-name}
588 * http://repo.or.cz/{project-name}.git