ByteUtil { public static long convertTextBytes(final String text) { try { Matcher m = ONE.matcher(text); if (m.matches()) { return Long.valueOf(Long.parseLong(m.group(1))); } m = KILO.matcher(text); if (m.matches()) { return Long.valueOf(Long.parseLong(m.group(1)) * 1024); } m = MEGA.matcher(text); if (m.matches()) { return Long.valueOf(Long.parseLong(m.group(1)) * 1024 * 1024); } m = GIGA.matcher(text); if (m.matches()) { return Long.valueOf(Long.parseLong(m.group(1)) * 1024 * 1024 * 1024); } return Long.parseLong(text); } catch (NumberFormatException e) { throw ActiveMQUtilBundle.BUNDLE.failedToParseLong(text); } }  static void debugFrame(Logger logger, String message, ByteBuf byteIn); static String formatGroup(String str, int groupSize, int lineBreak); static String maxString(String value, int size); static String bytesToHex(byte[] bytes); static String debugByteArray(byte[] byteArray); static String byteToChar(byte value); static String bytesToHex(byte[] bytes, int groupSize); static String toSimpleString(byte[] bytes); static final byte[] intToBytes(int value); static int bytesToInt(byte[] b); static byte[] longToBytes(long value); static void longToBytes(long x, byte[] output, int offset); static byte[] doubleLongToBytes(long value1, long value2); static byte[] hexToBytes(String hexStr); static String readLine(ActiveMQBuffer buffer); static long convertTextBytes(final String text); static boolean equals(final byte[] left, final byte[] right); static boolean equals(final byte[] left,
                                final byte[] right,
                                final int rightOffset,
                                final int rightLength); static boolean equals(final byte[] bytes, final ByteBuf byteBuf, final int offset, final int length); static int intFromBytes(byte b1, byte b2, byte b3, byte b4); static void zeros(final ByteBuffer buffer); static void zeros(final ByteBuffer buffer, int offset, int bytes); static String getHumanReadableByteCount(long bytes); static final String NON_ASCII_STRING; }
@Test public void testTextBytesToLongBytes() { long[] factor = new long[] {1, 5, 10}; String[] type = new String[]{"", "b", "k", "m", "g"}; long[] size = new long[]{1, 1, 1024, 1024 * 1024, 1024 * 1024 * 1024}; for (int i = 0; i < 3; i++) { for (int j = 0; j < type.length; j++) { assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + type[j])); assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + " " + type[j])); assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + type[j].toUpperCase())); assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + " " + type[j].toUpperCase())); if (j >= 2) { assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + type[j] + "b")); assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + " " + type[j] + "b")); assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + type[j].toUpperCase() + "B")); assertEquals(factor[i] * size[j], ByteUtil.convertTextBytes(factor[i] + " " + type[j].toUpperCase() + "B")); } } } }
@Test public void testTextBytesToLongBytesNegative() { try { ByteUtil.convertTextBytes("x"); fail(); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } }