@Test public void parseSpaceSizeTest() { long max = 10240; for (long k = 0; k < max; k ++) { Assert.assertEquals(k / 10, CommonUtils.parseSpaceSize(k / 10.0 + "b")); Assert.assertEquals(k / 10, CommonUtils.parseSpaceSize(k / 10.0 + "B")); Assert.assertEquals(k / 10, CommonUtils.parseSpaceSize(k / 10.0 + "")); } for (long k = 0; k < max; k ++) { Assert.assertEquals(k * Constants.KB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "kb")); Assert.assertEquals(k * Constants.KB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "Kb")); Assert.assertEquals(k * Constants.KB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "KB")); Assert.assertEquals(k * Constants.KB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "kB")); } for (long k = 0; k < max; k ++) { Assert.assertEquals(k * Constants.MB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "mb")); Assert.assertEquals(k * Constants.MB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "Mb")); Assert.assertEquals(k * Constants.MB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "MB")); Assert.assertEquals(k * Constants.MB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "mB")); } for (long k = 0; k < max; k ++) { Assert.assertEquals(k * Constants.GB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "gb")); Assert.assertEquals(k * Constants.GB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "Gb")); Assert.assertEquals(k * Constants.GB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "GB")); Assert.assertEquals(k * Constants.GB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "gB")); } for (long k = 0; k < max; k ++) { Assert.assertEquals(k * Constants.TB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "tb")); Assert.assertEquals(k * Constants.TB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "Tb")); Assert.assertEquals(k * Constants.TB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "TB")); Assert.assertEquals(k * Constants.TB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "tB")); } for (long k = 0; k < 8192; k ++) { Assert.assertEquals(k * Constants.PB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "pb")); Assert.assertEquals(k * Constants.PB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "Pb")); Assert.assertEquals(k * Constants.PB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "PB")); Assert.assertEquals(k * Constants.PB / 10, CommonUtils.parseSpaceSize(k / 10.0 + "pB")); } }
public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int tIndex = spaceSize.length() - 1; while (tIndex >= 0) { if (spaceSize.charAt(tIndex) > '9' || spaceSize.charAt(tIndex) < '0') { end = spaceSize.charAt(tIndex) + end; } else { break; } tIndex --; } spaceSize = spaceSize.substring(0, tIndex + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb")) { BigDecimal PBDecimal = new BigDecimal(Constants.PB); return PBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { runtimeException("Fail to parse " + ori + " as memory size"); return -1; } }
CommonUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int tIndex = spaceSize.length() - 1; while (tIndex >= 0) { if (spaceSize.charAt(tIndex) > '9' || spaceSize.charAt(tIndex) < '0') { end = spaceSize.charAt(tIndex) + end; } else { break; } tIndex --; } spaceSize = spaceSize.substring(0, tIndex + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb")) { BigDecimal PBDecimal = new BigDecimal(Constants.PB); return PBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { runtimeException("Fail to parse " + ori + " as memory size"); return -1; } } }
CommonUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int tIndex = spaceSize.length() - 1; while (tIndex >= 0) { if (spaceSize.charAt(tIndex) > '9' || spaceSize.charAt(tIndex) < '0') { end = spaceSize.charAt(tIndex) + end; } else { break; } tIndex --; } spaceSize = spaceSize.substring(0, tIndex + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb")) { BigDecimal PBDecimal = new BigDecimal(Constants.PB); return PBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { runtimeException("Fail to parse " + ori + " as memory size"); return -1; } }  }
CommonUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int tIndex = spaceSize.length() - 1; while (tIndex >= 0) { if (spaceSize.charAt(tIndex) > '9' || spaceSize.charAt(tIndex) < '0') { end = spaceSize.charAt(tIndex) + end; } else { break; } tIndex --; } spaceSize = spaceSize.substring(0, tIndex + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb")) { BigDecimal PBDecimal = new BigDecimal(Constants.PB); return PBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { runtimeException("Fail to parse " + ori + " as memory size"); return -1; } }  static String addLeadingZero(int number, int width); static void changeLocalFilePermission(String filePath, String perms); static void changeLocalFileToFullPermission(String filePath); static String cleanPath(String path); static ByteBuffer cloneByteBuffer(ByteBuffer buf); static List<ByteBuffer> cloneByteBufferList(List<ByteBuffer> source); static String concat(Object... args); static String convertByteArrayToStringWithoutEscape(byte[] data); static String convertMsToClockTime(long Millis); static String convertMsToDate(long Millis); static String convertMsToShortClockTime(long Millis); static String convertMsToSimpleDate(long Millis); static ByteBuffer generateNewByteBufferFromThriftRPCResults(ByteBuffer data); static long getBlockIdFromFileName(String name); static long getCurrentMs(); static long getCurrentNs(); static long getMB(long bytes); static String getName(String path); static String getParent(String path); static String[] getPathComponents(String path); static String getPathWithoutSchema(String path); static String getSizeFromBytes(long bytes); static void illegalArgumentException(Exception e); static void illegalArgumentException(String msg); static boolean isRoot(String path); static String listToString(List<T> list); static String parametersToString(Object... objs); static InetSocketAddress parseInetSocketAddress(String address); static long parseSpaceSize(String spaceSize); static void printByteBuffer(Logger LOG, ByteBuffer buf); static void printTimeTakenMs(long startTimeMs, Logger logger, String message); static void printTimeTakenNs(long startTimeNs, Logger logger, String message); static void runtimeException(Exception e); static void runtimeException(String msg); static void setLocalFileStickyBit(String file); static void sleepMs(Logger logger, long timeMs); static void tempoaryLog(String msg); static String[] toStringArray(ArrayList<String> src); static void touch(String path); static void validatePath(String path); }
CommonUtils { public static long parseSpaceSize(String spaceSize) { double alpha = 0.0001; String ori = spaceSize; String end = ""; int tIndex = spaceSize.length() - 1; while (tIndex >= 0) { if (spaceSize.charAt(tIndex) > '9' || spaceSize.charAt(tIndex) < '0') { end = spaceSize.charAt(tIndex) + end; } else { break; } tIndex --; } spaceSize = spaceSize.substring(0, tIndex + 1); double ret = Double.parseDouble(spaceSize); end = end.toLowerCase(); if (end.isEmpty() || end.equals("b")) { return (long) (ret + alpha); } else if (end.equals("kb")) { return (long) (ret * Constants.KB + alpha); } else if (end.equals("mb")) { return (long) (ret * Constants.MB + alpha); } else if (end.equals("gb")) { return (long) (ret * Constants.GB + alpha); } else if (end.equals("tb")) { return (long) (ret * Constants.TB + alpha); } else if (end.equals("pb")) { BigDecimal PBDecimal = new BigDecimal(Constants.PB); return PBDecimal.multiply(BigDecimal.valueOf(ret)).longValue(); } else { runtimeException("Fail to parse " + ori + " as memory size"); return -1; } }  static String addLeadingZero(int number, int width); static void changeLocalFilePermission(String filePath, String perms); static void changeLocalFileToFullPermission(String filePath); static String cleanPath(String path); static ByteBuffer cloneByteBuffer(ByteBuffer buf); static List<ByteBuffer> cloneByteBufferList(List<ByteBuffer> source); static String concat(Object... args); static String convertByteArrayToStringWithoutEscape(byte[] data); static String convertMsToClockTime(long Millis); static String convertMsToDate(long Millis); static String convertMsToShortClockTime(long Millis); static String convertMsToSimpleDate(long Millis); static ByteBuffer generateNewByteBufferFromThriftRPCResults(ByteBuffer data); static long getBlockIdFromFileName(String name); static long getCurrentMs(); static long getCurrentNs(); static long getMB(long bytes); static String getName(String path); static String getParent(String path); static String[] getPathComponents(String path); static String getPathWithoutSchema(String path); static String getSizeFromBytes(long bytes); static void illegalArgumentException(Exception e); static void illegalArgumentException(String msg); static boolean isRoot(String path); static String listToString(List<T> list); static String parametersToString(Object... objs); static InetSocketAddress parseInetSocketAddress(String address); static long parseSpaceSize(String spaceSize); static void printByteBuffer(Logger LOG, ByteBuffer buf); static void printTimeTakenMs(long startTimeMs, Logger logger, String message); static void printTimeTakenNs(long startTimeNs, Logger logger, String message); static void runtimeException(Exception e); static void runtimeException(String msg); static void setLocalFileStickyBit(String file); static void sleepMs(Logger logger, long timeMs); static void tempoaryLog(String msg); static String[] toStringArray(ArrayList<String> src); static void touch(String path); static void validatePath(String path);  }