CharsetEncoderUtil { public static ByteBuffer encode(String charsetName, CharBuffer charBuffer) { try { return encode(charsetName, CodingErrorAction.REPLACE, charBuffer); } catch (CharacterCodingException characterCodingException) { throw new Error(characterCodingException); } }  static ByteBuffer encode(String charsetName, CharBuffer charBuffer); static ByteBuffer encode(
			String charsetName, CodingErrorAction codingErrorAction,
			CharBuffer charBuffer); static CharsetEncoder getCharsetEncoder(String charsetName); static CharsetEncoder getCharsetEncoder(
		String charsetName, CodingErrorAction codingErrorAction);  }
@Test public void testEncode() throws Exception { Assert.assertEquals( ByteBuffer.wrap("abc".getBytes("US-ASCII")), CharsetEncoderUtil.encode( "US-ASCII", CodingErrorAction.REPORT, CharBuffer.wrap("abc"))); try { CharsetEncoderUtil.encode( "US-ASCII", CodingErrorAction.REPORT, CharBuffer.wrap("测试")); Assert.fail(); } catch (UnmappableCharacterException unmappableCharacterException) { Assert.assertEquals( 1, unmappableCharacterException.getInputLength()); } TestCharset testCharset = new TestCharset(); Object cache1 = ReflectionTestUtil.getAndSetFieldValue( Charset.class, "cache1", new Object[] {testCharset.name(), testCharset}); try { CharsetEncoderUtil.encode( testCharset.name(), CharBuffer.wrap(new char[0])); Assert.fail(); } catch (Error e) { Assert.assertSame( testCharset.getCharacterCodingException(), e.getCause()); } finally { ReflectionTestUtil.setFieldValue(Charset.class, "cache1", cache1); } }