Java Size stringToSize(String sizeString)

Here you can find the source of stringToSize(String sizeString)

Description

string To Size

License

Apache License

Declaration

public static long stringToSize(String sizeString) throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static long stringToSize(String sizeString) throws ParseException {
        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")) {
                return (long) (baseSize * 1024);
            } else if (unit.equals("m") || unit.equals("mb")) {
                return (long) (baseSize * (1024 * 1024));
            } else if (unit.equals("g") || unit.equals("gb")) {
                return (long) (baseSize * (1024 * 1024 * 1024));
            } else if (unit.equals("e") || unit.equals("eb")) {
                return (long) (baseSize * (1024L * 1024 * 1024 * 1024));
            }/*from w w w  .j  a  va 2s.  c om*/
        }
        throw new ParseException(sizeString, 0);
    }
}

Related

  1. readableSize(long size, DecimalFormat format)
  2. renderNumber(int n, int size)
  3. size(long l)
  4. size2human(long size)
  5. sizeRenderer(String value)
  6. toByte(String size)
  7. toHumanReadableSize(final long pSizeInBytes)
  8. toReadable(long size)