public class ByteSizeStrings
extends java.lang.Object
The builder()
presents a fluent interface to create
various configurations of formatting. The defaults produce metric units in
multiples of 1000 bytes at a precision of three significant figures. This is
the way disk space is normally measured, for example 128.1GB.
Alternatively the iso()
convenience method produces
ISO standard units in multiples of 1024 bytes, with the same precision as the
metric output. This is how RAM is normally measured, for example 12.4MiB
or 1.04GiB.
Finally, the java()
convenience method will produce
strings suitable for use with a Java command line, as part of the -Xms
or -Xmx
options. These output integer values only, so values up to
10GB will be reported in MB to preserve accuracy. For size values over 1000GB,
the output will still be formatted as GB but rounded to a mutiple of 1000.
The class is immutable and thread safe once built and a single instance of the three pre-defined configurations is created and returned buy the methods described above.
Modifier and Type | Class and Description |
---|---|
static class |
ByteSizeStrings.Builder
Configures and builds a
ByteSizeStrings formatter. |
Constructor and Description |
---|
ByteSizeStrings()
Deprecated.
Use
builder() |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
apply(java.lang.Long input)
A
Function implementation that formats its input using the current ByteSizeStrings values. |
static ByteSizeStrings.Builder |
builder()
Returns a builder for a
ByteSizeStrings formatter. |
java.util.Formattable |
formatted(long bytes)
Returns a
Formattable object that can be used with String.format(String, Object...) . |
static ByteSizeStrings |
iso()
Formats byte sizes using ISO standard suffixes and binary multiples of 1024
|
static ByteSizeStrings |
java()
Format byte sizes suitable for Java
-Xms arguments. |
java.lang.String |
makeSizeString(long size)
Format the size bytes as a String.
|
java.lang.String |
makeSizeString(long size,
int precision)
Format the size bytes as a String with the given precision.
|
static ByteSizeStrings |
metric()
Default byte size formatter using metric multiples of 1000.
|
static long |
parse(java.lang.String sizeString) |
static long |
parse(java.lang.String sizeString,
java.lang.String defaultUnits) |
static long |
parse(java.lang.String sizeStringOriginal,
java.lang.String defaultUnit,
ByteSizeStrings bytesMode)
parses the given string as a byte size string, e.g.
|
void |
setBytesPerMetricUnit(int bytesPerMetricUnit)
Deprecated.
|
void |
setLowerLimit(int lowerLimit)
Deprecated.
|
void |
setMaxLen(int maxLen)
Deprecated.
|
void |
setPrecision(int precision)
Deprecated.
|
void |
setSuffixBytes(java.lang.String suffixBytes)
Deprecated.
|
void |
setSuffixGiga(java.lang.String suffixGiga)
Deprecated.
|
void |
setSuffixKilo(java.lang.String suffixKilo)
Deprecated.
|
void |
setSuffixMega(java.lang.String suffixMega)
Deprecated.
|
void |
setSuffixTera(java.lang.String suffixTera)
Deprecated.
|
@Deprecated public ByteSizeStrings()
builder()
public static ByteSizeStrings.Builder builder()
ByteSizeStrings
formatter.public static final ByteSizeStrings java()
-Xms
arguments.public static ByteSizeStrings iso()
public static ByteSizeStrings metric()
@Deprecated public void setSuffixBytes(java.lang.String suffixBytes)
ByteSizeStrings.Builder.suffixBytes(String)
@Deprecated public void setSuffixKilo(java.lang.String suffixKilo)
ByteSizeStrings.Builder.suffixKilo(String)
@Deprecated public void setSuffixMega(java.lang.String suffixMega)
ByteSizeStrings.Builder.suffixMega(String)
@Deprecated public void setSuffixGiga(java.lang.String suffixGiga)
ByteSizeStrings.Builder.suffixGiga(String)
@Deprecated public void setSuffixTera(java.lang.String suffixTera)
ByteSizeStrings.Builder.suffixTera(String)
@Deprecated public void setBytesPerMetricUnit(int bytesPerMetricUnit)
ByteSizeStrings.Builder.bytesPerMetricUnit(int)
@Deprecated public void setMaxLen(int maxLen)
ByteSizeStrings.Builder.maxLen(int)
@Deprecated public void setPrecision(int precision)
ByteSizeStrings.Builder.precision(int)
@Deprecated public void setLowerLimit(int lowerLimit)
ByteSizeStrings.Builder.lowerLimit(int)
public java.lang.String makeSizeString(long size)
public java.lang.String makeSizeString(long size, int precision)
public java.util.Formattable formatted(long bytes)
Formattable
object that can be used with String.format(String, Object...)
.
When used as the argument for a %s format string element, the bytes value
will be formatted using the current ByteSizeStrings
values, or if the alternative
flag is set (using the %#s format string) it will use the metric()
formatter. Finally, the precision of the formatted value can be adjusted using format string
argumenbts like %.6s.
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
@Nullable public java.lang.String apply(@Nullable java.lang.Long input)
Function
implementation that formats its input using the current ByteSizeStrings
values.public static long parse(java.lang.String sizeString)
public static long parse(java.lang.String sizeString, java.lang.String defaultUnits)
public static long parse(java.lang.String sizeStringOriginal, java.lang.String defaultUnit, ByteSizeStrings bytesMode)
sizeString
- string to parsedefaultUnit
- optional units to append if a number (no units) are suppliedbytesMode
- optional evaluation mode to force 1024 or 1000 as the interpretation of the unit prefix;
if omitted, it will depend on the units supplied,
1000 for metric()
(e.g. "1kB"), and
1024 for java()
(e.g. "1k") and iso()
(e.g. "1KiB")