Skip to content

Latest commit

 

History

History
127 lines (107 loc) · 2.46 KB

readme.md

File metadata and controls

127 lines (107 loc) · 2.46 KB

human-bytes: human readable byte quantities

Intro

Package containing utilities to deal with the display of byte quantities.

Features include

  • a DSL to write human readable large quantities of bytes,
  • formatting byte quantities from a Long,
  • parsing byte quantities from a String.
Prefixes used for bytes (B)
Decimal (SI) Binary (IEC)
1000 k (kilo) 1024 Ki (kibi)
10002 M (mega) 10242 Mi (mebi)
10003 G (giga) 10243 Gi (gibi)
10004 T (tera) 10244 Ti (tebi)
10005 P (peta) 10245 Pi (pebi)
10006 E (exa) 10246 Ei (exbi)

See http://en.wikipedia.org/wiki/Byte for a discussion on the subject.

Note: The ambiguous JEDEC standard is not supported.

Usage

The DSL

import com.bizo.bytes._
// if you want to use the postfix notation (space between the value and the unit.)
import scala.language.postfixOps

val sizeSI = 10.kB
val sizeBinary = 10.KiB
// or with the postfix notation
val biggerSizeSI = 10 EB
val biggerSizeBinary = 10 EiB

Formatting a Long as byte quantities

import com.bizo.bytes._

println(SI(123456))  // prints 123.5kB
println(BIN(123456)) // prints 120.6KiB

Parsing byte quantities

import com.bizo.bytes._

val bytes = humanBytesToLong("123kB")  // 123000L
val bytes = humanBytesToLong("123KiB") // 125952L

Building

You need SBT 0.13 or higher.

# Compile and package jars.
sbt package

# Generate API documentation.
sbt doc

Target platform

  • Scala 2.10+

License

human-bytes is is licensed under the terms of the Apache Software License v2.0.

Copyright

Unless otherwise noted, all source files in this repository are Copyright (C) Bizo, inc. 2014