BufferedElementCountingOutputStream extends OutputStream { public void markElementStart() throws IOException { if (finished) { throw new IOException("Stream has been finished. Can not add any more elements."); } count++; } 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 testAddingElementWhenFinishedThrows() throws Exception { expectedException.expect(IOException.class); expectedException.expectMessage("Stream has been finished."); testValues(toBytes("a")).markElementStart(); }