BufferedElementCountingOutputStream extends OutputStream { @Override public void write(int b) throws IOException { if (finished) { throw new IOException("Stream has been finished. Can not write any more data."); } if (count == 0) { os.write(b); return; } if (buffer.hasRemaining()) { buffer.put((byte) b); } else { outputBuffer(); os.write(b); } } BufferedElementCountingOutputStream(OutputStream os);  BufferedElementCountingOutputStream(OutputStream os, long terminatorValue);   BufferedElementCountingOutputStream(OutputStream os, int bufferSize, long terminatorValue); void finish(); void markElementStart(); @Override void write(int b); @Override void write(byte[] b, int off, int len); @Override void flush(); @Override void close(); static final int DEFAULT_BUFFER_SIZE; }
@Test public void testWritingByteWhenFinishedThrows() throws Exception { expectedException.expect(IOException.class); expectedException.expectMessage("Stream has been finished."); testValues(toBytes("a")).write(1); }
@Test public void testWritingBytesWhenFinishedThrows() throws Exception { expectedException.expect(IOException.class); expectedException.expectMessage("Stream has been finished."); testValues(toBytes("a")).write("b".getBytes(Charsets.UTF_8)); }