public static long bytesFromReadableString()

in hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/util/UnitUtil.java [57:111]


    public static long bytesFromReadableString(String valueWithUnit) {
        int spacePos = valueWithUnit.indexOf(" ");
        E.checkArgument(spacePos >= 0,
                        "Invalid readable bytes '%s', " +
                        "expect format like '10 MB'", valueWithUnit);
        String unit = valueWithUnit.substring(spacePos + 1);

        long factor;
        switch (unit.trim().toUpperCase()) {
            case "B":
            case "BYTE":
            case "BYTES":
                factor = 1L;
                break;
            case "KB":
            case "KIB":
                factor = Bytes.KB;
                break;
            case "MB":
            case "MIB":
                factor = Bytes.MB;
                break;
            case "GB":
            case "GIB":
                factor = Bytes.GB;
                break;
            case "TB":
            case "TIB":
                factor = Bytes.TB;
                break;
            case "PB":
            case "PIB":
                factor = Bytes.PB;
                break;
            case "EB":
            case "EIB":
                factor = Bytes.EB;
                break;
            default:
                throw new IllegalArgumentException("Unrecognized unit " + unit);
        }

        double value;
        try {
            value = Double.parseDouble(valueWithUnit.substring(0, spacePos));
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format(
                      "Invalid parameter(not number): '%s'", valueWithUnit), e);
        }
        value = value * factor;
        E.checkArgument(value <= Long.MAX_VALUE,
                        "The value %s from parameter '%s' is out of range",
                        value, valueWithUnit);
        return (long) value;
    }