WriterOutputStream extends OutputStream { @Override public void write(byte[] bytes) throws IOException { write(bytes, 0, bytes.length); } WriterOutputStream(Writer writer);  WriterOutputStream(Writer writer, String charsetName);  WriterOutputStream(
		Writer writer, String charsetName, boolean autoFlush);  WriterOutputStream(
		Writer writer, String charsetName, int outputBufferSize);  WriterOutputStream(
		Writer writer, String charsetName, int outputBufferSize,
		boolean autoFlush); @Override void close(); @Override void flush(); String getEncoding(); @Override void write(byte[] bytes); @Override void write(byte[] bytes, int offset, int length); @Override void write(int b);  }
@Test public void testWrite() throws IOException { CharArrayWriter charArrayWriter = new CharArrayWriter(); WriterOutputStream writerOutputStream = new WriterOutputStream( charArrayWriter, StringPool.UTF8, true); int charNumber = 0; String unalignedOutput = "非对齐测试中文输出"; byte[] unalignedInput = unalignedOutput.getBytes(StringPool.UTF8); for (byte b : unalignedInput) { writerOutputStream.write(b); int currentCharNumber = charArrayWriter.size(); if (currentCharNumber > charNumber) { charNumber = currentCharNumber; Assert.assertEquals( unalignedOutput.charAt(charNumber - 1), charArrayWriter.toCharArray()[charNumber - 1]); } } Assert.assertEquals(unalignedOutput, charArrayWriter.toString()); }
@Test public void testWriteBlockUnaligned() throws IOException { CharArrayWriter charArrayWriter = new CharArrayWriter(); String unalignedOutput = "非对齐测试中文输出"; try (WriterOutputStream writerOutputStream = new WriterOutputStream( charArrayWriter, StringPool.UTF8, true)) { byte[] unalignedInput = unalignedOutput.getBytes(StringPool.UTF8); writerOutputStream.write(unalignedInput[0]); writerOutputStream.write( unalignedInput, 1, unalignedInput.length - 2); writerOutputStream.write(unalignedInput[unalignedInput.length - 1]); } Assert.assertEquals(unalignedOutput, charArrayWriter.toString()); }
@Test public void testWriteError() { WriterOutputStream writerOutputStream = new WriterOutputStream( new DummyWriter(), "US-ASCII"); CharsetDecoder charsetDecoder = ReflectionTestUtil.getFieldValue( writerOutputStream, "_charsetDecoder"); charsetDecoder.onMalformedInput(CodingErrorAction.REPORT); try { writerOutputStream.write(new byte[] {-1, -2, -3, -4}); Assert.fail(); } catch (IOException ioException) { Assert.assertEquals( "Unexcepted coder result MALFORMED[1]", ioException.getMessage()); } }