@Test public void parseSpaceSize() { long max = 10240; for (long k = 0; k < max; k++) { assertEquals(k / 10, FormatUtils.parseSpaceSize(k / 10.0 + "b")); assertEquals(k / 10, FormatUtils.parseSpaceSize(k / 10.0 + "B")); assertEquals(k / 10, FormatUtils.parseSpaceSize(k / 10.0 + "")); } for (long k = 0; k < max; k++) { assertEquals(k * Constants.KB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "kb")); assertEquals(k * Constants.KB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "Kb")); assertEquals(k * Constants.KB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "KB")); assertEquals(k * Constants.KB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "kB")); assertEquals(k * Constants.KB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "k")); assertEquals(k * Constants.KB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "K")); } for (long k = 0; k < max; k++) { assertEquals(k * Constants.MB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "mb")); assertEquals(k * Constants.MB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "Mb")); assertEquals(k * Constants.MB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "MB")); assertEquals(k * Constants.MB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "mB")); assertEquals(k * Constants.MB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "m")); assertEquals(k * Constants.MB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "M")); } for (long k = 0; k < max; k++) { assertEquals(k * Constants.GB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "gb")); assertEquals(k * Constants.GB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "Gb")); assertEquals(k * Constants.GB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "GB")); assertEquals(k * Constants.GB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "gB")); assertEquals(k * Constants.GB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "g")); assertEquals(k * Constants.GB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "G")); } for (long k = 0; k < max; k++) { assertEquals(k * Constants.TB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "tb")); assertEquals(k * Constants.TB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "Tb")); assertEquals(k * Constants.TB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "TB")); assertEquals(k * Constants.TB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "tB")); assertEquals(k * Constants.TB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "t")); assertEquals(k * Constants.TB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "T")); } for (long k = 0; k < 8192; k++) { assertEquals(k * Constants.PB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "pb")); assertEquals(k * Constants.PB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "Pb")); assertEquals(k * Constants.PB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "PB")); assertEquals(k * Constants.PB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "pB")); assertEquals(k * Constants.PB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "p")); assertEquals(k * Constants.PB / 10, FormatUtils.parseSpaceSize(k / 10.0 + "P")); } }
public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int index = spaceSize.length() - 1; while (index >= 0) { if (spaceSize.charAt(index) > '9' || spaceSize.charAt(index) < '0') { end = spaceSize.charAt(index) + end; } else { break; } index--; } spaceSize = spaceSize.substring(0, index + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb") || end.equals("k")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb") || end.equals("m")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb") || end.equals("g")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb") || end.equals("t")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb") || end.equals("p")) { BigDecimal pBDecimal = new BigDecimal(Constants.PB); return pBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { throw new IllegalArgumentException("Fail to parse " + ori + " to bytes"); } }
FormatUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int index = spaceSize.length() - 1; while (index >= 0) { if (spaceSize.charAt(index) > '9' || spaceSize.charAt(index) < '0') { end = spaceSize.charAt(index) + end; } else { break; } index--; } spaceSize = spaceSize.substring(0, index + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb") || end.equals("k")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb") || end.equals("m")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb") || end.equals("g")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb") || end.equals("t")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb") || end.equals("p")) { BigDecimal pBDecimal = new BigDecimal(Constants.PB); return pBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { throw new IllegalArgumentException("Fail to parse " + ori + " to bytes"); } } }
FormatUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int index = spaceSize.length() - 1; while (index >= 0) { if (spaceSize.charAt(index) > '9' || spaceSize.charAt(index) < '0') { end = spaceSize.charAt(index) + end; } else { break; } index--; } spaceSize = spaceSize.substring(0, index + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb") || end.equals("k")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb") || end.equals("m")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb") || end.equals("g")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb") || end.equals("t")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb") || end.equals("p")) { BigDecimal pBDecimal = new BigDecimal(Constants.PB); return pBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { throw new IllegalArgumentException("Fail to parse " + ori + " to bytes"); } } private  FormatUtils(); }
FormatUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int index = spaceSize.length() - 1; while (index >= 0) { if (spaceSize.charAt(index) > '9' || spaceSize.charAt(index) < '0') { end = spaceSize.charAt(index) + end; } else { break; } index--; } spaceSize = spaceSize.substring(0, index + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb") || end.equals("k")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb") || end.equals("m")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb") || end.equals("g")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb") || end.equals("t")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb") || end.equals("p")) { BigDecimal pBDecimal = new BigDecimal(Constants.PB); return pBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { throw new IllegalArgumentException("Fail to parse " + ori + " to bytes"); } } private  FormatUtils(); static String parametersToString(Object... objs); static String byteBufferToString(ByteBuffer buf); static String byteArrayToHexString(byte[] bytes); static String byteArrayToHexString(byte[] bytes, String prefix, String separator); static String formatTimeTakenMs(long startTimeMs, String message); static String formatTimeTakenNs(long startTimeNs, String message); static String getSizeFromBytes(long bytes); static long parseSpaceSize(String spaceSize); static long parseTimeSize(String timeSize); static String formatMode(short mode, boolean directory, boolean hasExtended); }
FormatUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int index = spaceSize.length() - 1; while (index >= 0) { if (spaceSize.charAt(index) > '9' || spaceSize.charAt(index) < '0') { end = spaceSize.charAt(index) + end; } else { break; } index--; } spaceSize = spaceSize.substring(0, index + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb") || end.equals("k")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb") || end.equals("m")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb") || end.equals("g")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb") || end.equals("t")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb") || end.equals("p")) { BigDecimal pBDecimal = new BigDecimal(Constants.PB); return pBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { throw new IllegalArgumentException("Fail to parse " + ori + " to bytes"); } } private  FormatUtils(); static String parametersToString(Object... objs); static String byteBufferToString(ByteBuffer buf); static String byteArrayToHexString(byte[] bytes); static String byteArrayToHexString(byte[] bytes, String prefix, String separator); static String formatTimeTakenMs(long startTimeMs, String message); static String formatTimeTakenNs(long startTimeNs, String message); static String getSizeFromBytes(long bytes); static long parseSpaceSize(String spaceSize); static long parseTimeSize(String timeSize); static String formatMode(short mode, boolean directory, boolean hasExtended);  }