OutputStreamWriter extends Writer { @Override public void close() throws IOException { if (!_open) { return; } try { _flushEncoder(); _flushBuffer(); _outputStream.close(); } finally { _open = false; } } OutputStreamWriter(OutputStream outputStream);  OutputStreamWriter(OutputStream outputStream, String charsetName);  OutputStreamWriter(
		OutputStream outputStream, String charsetName, boolean autoFlush);  OutputStreamWriter(
		OutputStream outputStream, String charsetName, int outputBufferSize);  OutputStreamWriter(
		OutputStream outputStream, String charsetName, int outputBufferSize,
		boolean autoFlush); @Override void close(); @Override void flush(); String getEncoding(); @Override void write(char[] chars); @Override void write(char[] chars, int offset, int length); @Override void write(int c); @Override void write(String string); @Override void write(String string, int offset, int length);  }
@Test public void testClose() throws IOException { MarkerOutputStream markerOutputStream = new MarkerOutputStream(); try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter( markerOutputStream)) { Assert.assertFalse(markerOutputStream._closed); outputStreamWriter.close(); Assert.assertTrue(markerOutputStream._closed); markerOutputStream._closed = false; try { outputStreamWriter.write(0); Assert.fail(); } catch (IOException ioException) { Assert.assertEquals("Stream closed", ioException.getMessage()); } } Assert.assertFalse(markerOutputStream._closed); final IOException ioException1 = new IOException(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter( new UnsyncByteArrayOutputStream() { @Override public void close() throws IOException { throw ioException1; } }); try { outputStreamWriter.close(); Assert.fail(); } catch (IOException ioException2) { Assert.assertSame(ioException2, ioException1); } outputStreamWriter.close(); }
@Test public void testFlushEncoder() throws IOException { MarkerOutputStream markerOutputStream = new MarkerOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter( markerOutputStream); final AtomicInteger flushCounter = new AtomicInteger(); ReflectionTestUtil.setFieldValue( outputStreamWriter, "_charsetEncoder", new CharsetEncoderWrapper( ReflectionTestUtil.getFieldValue( outputStreamWriter, "_charsetEncoder")) { @Override protected CoderResult implFlush(ByteBuffer out) { int count = flushCounter.getAndIncrement(); if (count == 0) { return CoderResult.OVERFLOW; } return super.implFlush(out); } }); outputStreamWriter.close(); Assert.assertEquals(2, flushCounter.get()); outputStreamWriter = new OutputStreamWriter(markerOutputStream); ReflectionTestUtil.setFieldValue( outputStreamWriter, "_charsetEncoder", new CharsetEncoderWrapper( ReflectionTestUtil.getFieldValue( outputStreamWriter, "_charsetEncoder")) { @Override protected CoderResult implFlush(ByteBuffer out) { return CoderResult.malformedForLength(1); } }); try { outputStreamWriter.close(); } catch (MalformedInputException malformedInputException) { Assert.assertEquals(1, malformedInputException.getInputLength()); } }