ByteArrayCharSequence implements CharSequence { public static byte[] toSingleByteUtf8Encoding(CharSequence charSequence) { final int length = charSequence.length(); byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { char inputChar = charSequence.charAt(i); if (inputChar > 255) { throw new IncompatibleCharacterException("Input contains a character which cannot be represented as a single byte in UTF-8: " + inputChar); } bytes[i] = (byte)inputChar; } return bytes; } ByteArrayCharSequence(byte[] bytes, int start, int end); @Override int length(); @Override char charAt(int index); @Override ByteArrayCharSequence subSequence(int start, int end); @Override String toString(); static byte[] toSingleByteUtf8Encoding(CharSequence charSequence);  }
@Test(expected = IllegalStateException.class) public void testEncodeUtf8_UnsupportedChar() throws Exception { ByteArrayCharSequence.toSingleByteUtf8Encoding("FOOBAR○"); }