@Test public void shouldParseSizesWithSuffix() { assertThat(parseSize("", "1"), is(1L)); assertThat(parseSize("", "1k"), is(1024L)); assertThat(parseSize("", "1K"), is(1024L)); assertThat(parseSize("", "1m"), is(1024L * 1024)); assertThat(parseSize("", "1M"), is(1024L * 1024)); assertThat(parseSize("", "1g"), is(1024L * 1024 * 1024)); assertThat(parseSize("", "1G"), is(1024L * 1024 * 1024)); }
public static long parseSize(final String propertyName, final String propertyValue) { final int lengthMinusSuffix = propertyValue.length() - 1; final char lastCharacter = propertyValue.charAt(lengthMinusSuffix); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, lengthMinusSuffix); switch (lastCharacter) { case 'k': case 'K': if (value > MAX_K_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024; case 'm': case 'M': if (value > MAX_M_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024; case 'g': case 'G': if (value > MAX_G_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024 * 1024; default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: k, m, or g."); } }
SystemUtil { public static long parseSize(final String propertyName, final String propertyValue) { final int lengthMinusSuffix = propertyValue.length() - 1; final char lastCharacter = propertyValue.charAt(lengthMinusSuffix); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, lengthMinusSuffix); switch (lastCharacter) { case 'k': case 'K': if (value > MAX_K_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024; case 'm': case 'M': if (value > MAX_M_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024; case 'g': case 'G': if (value > MAX_G_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024 * 1024; default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: k, m, or g."); } } }
SystemUtil { public static long parseSize(final String propertyName, final String propertyValue) { final int lengthMinusSuffix = propertyValue.length() - 1; final char lastCharacter = propertyValue.charAt(lengthMinusSuffix); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, lengthMinusSuffix); switch (lastCharacter) { case 'k': case 'K': if (value > MAX_K_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024; case 'm': case 'M': if (value > MAX_M_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024; case 'g': case 'G': if (value > MAX_G_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024 * 1024; default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: k, m, or g."); } } private  SystemUtil(); }
SystemUtil { public static long parseSize(final String propertyName, final String propertyValue) { final int lengthMinusSuffix = propertyValue.length() - 1; final char lastCharacter = propertyValue.charAt(lengthMinusSuffix); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, lengthMinusSuffix); switch (lastCharacter) { case 'k': case 'K': if (value > MAX_K_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024; case 'm': case 'M': if (value > MAX_M_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024; case 'g': case 'G': if (value > MAX_G_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024 * 1024; default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: k, m, or g."); } } private  SystemUtil(); static String osName(); static long getPid(); static boolean isWindows(); static boolean isLinux(); static boolean isDebuggerAttached(); static String tmpDirName(); static String threadDump(); static void loadPropertiesFile(final String filenameOrUrl); static void loadPropertiesFiles(final String[] filenamesOrUrls); static int getSizeAsInt(final String propertyName, final int defaultValue); static long getSizeAsLong(final String propertyName, final long defaultValue); static long parseSize(final String propertyName, final String propertyValue); static long getDurationInNanos(final String propertyName, final long defaultValue); static long parseDuration(final String propertyName, final String propertyValue); }
SystemUtil { public static long parseSize(final String propertyName, final String propertyValue) { final int lengthMinusSuffix = propertyValue.length() - 1; final char lastCharacter = propertyValue.charAt(lengthMinusSuffix); if (Character.isDigit(lastCharacter)) { return Long.parseLong(propertyValue); } final long value = AsciiEncoding.parseLongAscii(propertyValue, 0, lengthMinusSuffix); switch (lastCharacter) { case 'k': case 'K': if (value > MAX_K_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024; case 'm': case 'M': if (value > MAX_M_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024; case 'g': case 'G': if (value > MAX_G_VALUE) { throw new NumberFormatException(propertyName + " would overflow long: " + propertyValue); } return value * 1024 * 1024 * 1024; default: throw new NumberFormatException( propertyName + ": " + propertyValue + " should end with: k, m, or g."); } } private  SystemUtil(); static String osName(); static long getPid(); static boolean isWindows(); static boolean isLinux(); static boolean isDebuggerAttached(); static String tmpDirName(); static String threadDump(); static void loadPropertiesFile(final String filenameOrUrl); static void loadPropertiesFiles(final String[] filenamesOrUrls); static int getSizeAsInt(final String propertyName, final int defaultValue); static long getSizeAsLong(final String propertyName, final long defaultValue); static long parseSize(final String propertyName, final String propertyValue); static long getDurationInNanos(final String propertyName, final long defaultValue); static long parseDuration(final String propertyName, final String propertyValue); static final long PID_NOT_FOUND; }