Java Utililty Methods Size

List of utility methods to do Size

Description

The list of methods to do Size are organized into topic(s).

Method

StringsizeRenderer(String value)
size Renderer
Double size = Double.parseDouble(value);
String text = "";
Double val;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
if (size >= 1 && size < 1024) {
    text = size + " Bytes";
} else if (size > 1024 && size < 1048576) {
    val = (size / 1024);
...
longstringToSize(String sizeString)
string To Size
Pattern pattern = Pattern.compile("(-?\\d+\\.?\\d*)([\\w]{0,2})", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sizeString);
if (matcher.matches()) {
    double baseSize = Double.parseDouble(matcher.group(1));
    String unit = matcher.group(2).toLowerCase();
    if (unit.equals("b") || unit.length() == 0) {
        return (long) baseSize;
    } else if (unit.equals("k") || unit.equals("kb")) {
...
longtoByte(String size)
to Byte
Matcher matcher = SIZE_PATTERN.matcher(size);
if (!matcher.matches()) {
    throw new ParseException("Invalid size. Use 1024, 3.5M or 1gb", 0);
int factor = 1;
if (matcher.group(3) != null) {
    String unit = matcher.group(3).toUpperCase();
    if (unit.length() == 1 && !unit.equals("B")) {
...
StringtoHumanReadableSize(final long pSizeInBytes)
Formats the given number to a human readable format.
if (pSizeInBytes < 1024L) {
    return pSizeInBytes + " Bytes";
} else if (pSizeInBytes < (1024L << 10)) {
    return getSizeFormat().format(pSizeInBytes / (double) (1024L)) + " KB";
} else if (pSizeInBytes < (1024L << 20)) {
    return getSizeFormat().format(pSizeInBytes / (double) (1024L << 10)) + " MB";
} else if (pSizeInBytes < (1024L << 30)) {
    return getSizeFormat().format(pSizeInBytes / (double) (1024L << 20)) + " GB";
...
StringtoReadable(long size)
to Readable
if (size <= 0) {
    return "0B";
if (size < 1024) {
    return size + "B";
String[] sizeUnits = new String[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
int unitIndex = 0;
...