ByteUtil { public static void asciiEncode(final CharSequence chars, final int charOffset, final byte[] buffer, final int bufferOffset, final int length) { for (int i = 0; i < length; i++) { int charValue = chars.charAt(i + charOffset); if (charValue > 127) { throw new IllegalArgumentException("Character " + chars.charAt(i + charOffset) + " is not " + StandardCharsets.US_ASCII); } buffer[i + bufferOffset] = (byte) charValue; } }  static void writeLongAsAscii(final byte[] outputBuffer, final int offset, final long valueParam); static boolean isAsciiDigit(final byte asciiCharacter); static String formatIntAsIpAddress(final int address); static void formatIntAsIpAddress(final StringBuilder buffer, final int address); static long packLongWithUnsignedShortInts(final int int1, final int int2, final int int3, final int int4); static boolean isEqual(final byte[] lhs, final int lhsOffset, final byte[] rhs, final int rhsOffset, final int length); static int readIntFromAscii(final byte[] asciiBuffer, final int offset, final int length); static long readLongFromAscii(final byte[] asciiBuffer, final int offset, final int length); static boolean isInteger(final byte[] asciiBuffer, final int offset, final int length); static void replace(final byte[] buffer, final int offset, final int length,
                               final byte target, final byte replacement); static void asciiEncode(final CharSequence chars, final int charOffset,
                                   final byte[] buffer, final int bufferOffset, final int length); static final int MAX_UNSIGNED_SHORT; }
@Test public void shouldEncodeStringAsAscii() throws UnsupportedEncodingException { final String testSequence = "This is a string of characters that should encode into ascii bytes as simple as 123."; final byte[] result = new byte[testSequence.length()]; ByteUtil.asciiEncode(testSequence, 0, result, 0, result.length); assertArrayEquals(testSequence.getBytes(StandardCharsets.US_ASCII), result); }
@Test(expected = IllegalArgumentException.class) public void shouldOnlyAllowUsAscii() { final String testSequence = "The £ symbol is not allowed in US-ASCII"; final byte[] result = new byte[testSequence.length()]; ByteUtil.asciiEncode(testSequence, 0, result, 0, result.length); }