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ß <https://github.com/sebhoss[@sebhoss]>
4 :project-name: storage-units
5 :project-group: de.xn--ho-hia.utils.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.utils.storage_units/storage-units"><img src="https://img.shields.io/maven-central/v/de.xn--ho-hia.utils.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.utils.storage_units/storage-units"><img src="https://www.javadoc.io/badge/de.xn--ho-hia.utils.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 units according to link:http://en.wikipedia.org/wiki/ISO/IEC_80000[ISO IEC 80000-13:2008].
29 * Type safe object model for storage units
30 * Factories to create those objects
31 * Basic math operators
32 * Comparisons and equality
33 * Lossless conversion between all units
34 * Human readable text format, including custom formats.
38 .Binary-prefixed units
40 | Name | Symbol | Exponential | Absolute
60 | 1 099 511 627 776 Byte
65 | 1 125 899 906 842 624 Byte
70 | 1 152 921 504 606 846 976 Byte
75 | 1 180 591 620 717 411 303 424 Byte
80 | 1 208 925 819 614 629 174 706 176 Byte
83 .Metric-prefixed units
85 | Name | Symbol | Exponential | Absolute
105 | 1 000 000 000 000 Byte
110 | 1 000 000 000 000 000 Byte
115 | 1 000 000 000 000 000 000 Byte
120 | 1 000 000 000 000 000 000 000 Byte
125 | 1 000 000 000 000 000 000 000 000 Byte
128 === Development Status
130 All units according to ISO IEC 80000-13:2008 are implemented. This project is in maintenance mode.
137 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.
142 Kilobyte unit = Kilobyte.valueOf(2500) // 2 500 Byte or "2.50 kB"
143 Kibibyte unit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"
144 Megabyte unit = Megabyte.valueOf(1000000) // 1 000 000 Byte or "1.00 MB"
146 // 'BigInteger' based
147 Kilobyte unit = Kilobyte.valueOf(BigInteger.valueOf(2500)) // 2 500 Byte or "2.50 kB"
148 Kibibyte unit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"
149 Megabyte unit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"
152 The `StorageUnits` class offers two factory methods that automatically pick the best-matching unit for a given number of bytes.
159 StorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)
160 StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)
162 // 'BigInteger' based
163 StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)
164 StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)
172 StorageUnit<?> unit = StorageUnits.metricValueOf(120000) // Kilobyte (120.00 kB)
173 StorageUnit<?> unit = StorageUnits.metricValueOf(1000000) // Megabyte (1.00 MB)
175 // 'BigInteger' based
176 StorageUnit<?> unit = StorageUnits.metricValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)
177 StorageUnit<?> unit = StorageUnits.metricValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)
180 Additionally high-level factory methods are also available in the `StorageUnits` class.
184 Kibibyte unit = StorageUnits.kibibyte(1) // 1 024 Byte
185 Mebibyte unit = StorageUnits.mebibyte(1) // 1 048 576 Byte
186 Gibibyte unit = StorageUnits.gibibyte(1) // 1 073 741 824 Byte
187 Tebibyte unit = StorageUnits.tebibyte(1) // 1 099 511 627 776 Byte
188 Pebibyte unit = StorageUnits.pebibyte(1) // 1 125 899 906 842 624 Byte
189 Exbibyte unit = StorageUnits.exbibyte(1) // 1 152 921 504 606 846 976 Byte
190 Zebibyte unit = StorageUnits.zebibyte(1) // 1 180 591 620 717 411 303 424 Byte
191 Yobibyte unit = StorageUnits.yobibyte(1) // 1 208 925 819 614 629 174 706 176 Byte
193 Kilobyte unit = StorageUnits.kilobyte(1) // 1 000 Byte
194 Megabyte unit = StorageUnits.megabyte(1) // 1 000 000 Byte
195 Gigabyte unit = StorageUnits.gigabyte(1) // 1 000 000 000 Byte
196 Terabyte unit = StorageUnits.terabyte(1) // 1 000 000 000 000 Byte
197 Petabyte unit = StorageUnits.petabyte(1) // 1 000 000 000 000 000 Byte
198 Exabyte unit = StorageUnits.exabyte(1) // 1 000 000 000 000 000 000 Byte
199 Zettabyte unit = StorageUnits.zettabyte(1) // 1 000 000 000 000 000 000 000 Byte
200 Yottabyte unit = StorageUnits.yottabyte(1) // 1 000 000 000 000 000 000 000 000 Byte
203 === Add, Subtract, Multiply, Divide
205 Each unit implements the basic four math operations. All operations retain their original type, e.g. `[Kilobyte] + [Megabyte] = [Kilobyte]`
209 kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Byte
210 kibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Byte
211 kibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Byte
212 megabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Byte
213 gigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyte
214 terabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte
217 === Comparison & Equality
219 Each unit is comparable to each other unit.
223 kibibyte(1024).compareTo(mebibyte(1)) == 0 // true
224 kibibyte(1000).compareTo(mebibyte(1)) == 0 // false
225 petabyte(3).compareTo(terabyte(3000)) == 0 // true
227 megabyte(1000).equals(gigabyte(1)) // true
228 megabyte(1024).equals(gigabyte(1)) // false
229 terabyte(12).equals(gigabyte(12000)) // true
234 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.
238 // default pattern '0.00'
239 terabyte(2).toString() // "2.00 TB"
240 gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"
241 petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"
243 // use custom pattern
244 kilobyte(212345).toString("0.0") // "212345.0 kB"
245 gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"
246 kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"
249 Format customFormat = new DecimalFormat("#.00000");
250 terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"
255 Each unit can be converted to each other unit.
259 Megabyte unit = kilobyte(1000).asMegabyte() // "1.00 MB"
260 Kilobyte unit = gigabyte(12).asKilobyte() // "12000000.00 kB"
261 Gigabyte unit = terabyte(1).asGigabyte() // "1000.00 GB"
264 Each unit can be expressed as a fraction of another unit.
268 BigDecimal kilobytes = megabyte(1).inKilobyte() // 1 000
269 BigDecimal bytes = kibibyte(2).inByte() // 2 048
270 BigDecimal terabytes = gigabyte(15).inTerabyte() // 0.015
275 To use this project just declare the following dependency inside your POM:
277 [source,xml,subs="attributes,verbatim"]
281 <groupId>{project-group}</groupId>
282 <artifactId>{project-name}</artifactId>
283 <version>${version.storage-units}</version>
288 Replace `${version.storage-units}` with the link:++http://search.maven.org/#search%7Cga%7C1%7Cg%3Ade.xn--ho-hia.utils.storage_units%20a%3Astorage-units++[latest release]. This project follows the link:http://semver.org/[semantic versioning guidelines].
292 This project is compatible with the following Java versions:
296 | | 1.X.Y | 2.X.Y | 3.X.Y
311 Originally inspired by link:https://github.com/twitter/util#space[Twitters util] package.
315 To the extent possible under law, the author(s) have dedicated all copyright
316 and related and neighboring rights to this software to the public domain
317 worldwide. This software is distributed without any warranty.
319 You should have received a copy of the CC0 Public Domain Dedication along
320 with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/.
324 * https://github.com/sebhoss/{project-name}
325 * https://bitbucket.org/sebhoss/{project-name}
326 * https://gitlab.com/sebastian.hoss/{project-name}
327 * http://v2.pikacode.com/sebhoss/{project-name}
328 * http://repo.or.cz/{project-name}.git