@Test public void testHumanReadableToByteCount() throws Exception { Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("10"), 10L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1k"), 1024L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1m"), 1048576L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1g"), 1073741824L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1t"), 1099511627776L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1K"), 1024L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1kb"), 1024L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("1KB"), 1024L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("2k"), 2048L); Assert.assertEquals(StringParsingUtils.humanReadableToByteCount("2.5k"), 2560L); }
public static long humanReadableToByteCount(String string) throws FormatException { Matcher matcher = HUMAN_READABLE_SIZE_PATTERN.matcher(string.trim()); if (!matcher.matches()) { throw new FormatException("Could not parse human readable size string " + string); } int exponent = 0; switch (matcher.group(2).toUpperCase()) { case "": exponent = 0; break; case "K": exponent = 10; break; case "M": exponent = 20; break; case "G": exponent = 30; break; case "T": exponent = 40; break; case "P": exponent = 50; break; default: throw new FormatException("Could not parse human readable size string " + string); } try { double base = Double.parseDouble(matcher.group(1)); return (long) (base * (1L << exponent)); } catch (NumberFormatException nfe) { throw new FormatException("Could not parse human readable size string " + string); } }
StringParsingUtils { public static long humanReadableToByteCount(String string) throws FormatException { Matcher matcher = HUMAN_READABLE_SIZE_PATTERN.matcher(string.trim()); if (!matcher.matches()) { throw new FormatException("Could not parse human readable size string " + string); } int exponent = 0; switch (matcher.group(2).toUpperCase()) { case "": exponent = 0; break; case "K": exponent = 10; break; case "M": exponent = 20; break; case "G": exponent = 30; break; case "T": exponent = 40; break; case "P": exponent = 50; break; default: throw new FormatException("Could not parse human readable size string " + string); } try { double base = Double.parseDouble(matcher.group(1)); return (long) (base * (1L << exponent)); } catch (NumberFormatException nfe) { throw new FormatException("Could not parse human readable size string " + string); } } }
StringParsingUtils { public static long humanReadableToByteCount(String string) throws FormatException { Matcher matcher = HUMAN_READABLE_SIZE_PATTERN.matcher(string.trim()); if (!matcher.matches()) { throw new FormatException("Could not parse human readable size string " + string); } int exponent = 0; switch (matcher.group(2).toUpperCase()) { case "": exponent = 0; break; case "K": exponent = 10; break; case "M": exponent = 20; break; case "G": exponent = 30; break; case "T": exponent = 40; break; case "P": exponent = 50; break; default: throw new FormatException("Could not parse human readable size string " + string); } try { double base = Double.parseDouble(matcher.group(1)); return (long) (base * (1L << exponent)); } catch (NumberFormatException nfe) { throw new FormatException("Could not parse human readable size string " + string); } }  }
StringParsingUtils { public static long humanReadableToByteCount(String string) throws FormatException { Matcher matcher = HUMAN_READABLE_SIZE_PATTERN.matcher(string.trim()); if (!matcher.matches()) { throw new FormatException("Could not parse human readable size string " + string); } int exponent = 0; switch (matcher.group(2).toUpperCase()) { case "": exponent = 0; break; case "K": exponent = 10; break; case "M": exponent = 20; break; case "G": exponent = 30; break; case "T": exponent = 40; break; case "P": exponent = 50; break; default: throw new FormatException("Could not parse human readable size string " + string); } try { double base = Double.parseDouble(matcher.group(1)); return (long) (base * (1L << exponent)); } catch (NumberFormatException nfe) { throw new FormatException("Could not parse human readable size string " + string); } }  static long humanReadableToByteCount(String string); }
StringParsingUtils { public static long humanReadableToByteCount(String string) throws FormatException { Matcher matcher = HUMAN_READABLE_SIZE_PATTERN.matcher(string.trim()); if (!matcher.matches()) { throw new FormatException("Could not parse human readable size string " + string); } int exponent = 0; switch (matcher.group(2).toUpperCase()) { case "": exponent = 0; break; case "K": exponent = 10; break; case "M": exponent = 20; break; case "G": exponent = 30; break; case "T": exponent = 40; break; case "P": exponent = 50; break; default: throw new FormatException("Could not parse human readable size string " + string); } try { double base = Double.parseDouble(matcher.group(1)); return (long) (base * (1L << exponent)); } catch (NumberFormatException nfe) { throw new FormatException("Could not parse human readable size string " + string); } }  static long humanReadableToByteCount(String string); static final Pattern HUMAN_READABLE_SIZE_PATTERN; }