BoundedByteArrayOutputStream extends OutputStream { protected void resetBuffer(byte[] buf, int offset, int limit) { int capacity = buf.length - offset; if ((capacity < limit) || (capacity | limit) < 0) { throw new IllegalArgumentException("Invalid capacity/limit"); } this.buffer = buf; this.startOffset = offset; this.currentPointer = offset; this.limit = offset + limit; } BoundedByteArrayOutputStream(int capacity);  BoundedByteArrayOutputStream(int capacity, int limit); protected  BoundedByteArrayOutputStream(byte[] buf, int offset, int limit); @Override void write(int b); @Override void write(byte b[], int off, int len); void reset(int newlim); void reset(); int getLimit(); byte[] getBuffer(); int size(); int available();  }
@Test public void testResetBuffer() throws IOException { ResettableBoundedByteArrayOutputStream stream = new ResettableBoundedByteArrayOutputStream(SIZE); stream.write(INPUT, 0, SIZE); assertTrue("Array Contents Mismatch", Arrays.equals(INPUT, stream.getBuffer())); boolean caughtException = false; try { stream.write(INPUT[0]); } catch (Exception e) { caughtException = true; } assertTrue("Writing beyond limit did not throw an exception", caughtException); byte[] newBuf = new byte[SIZE]; stream.resetBuffer(newBuf, 0, newBuf.length); assertTrue("Limit did not get reset correctly", (stream.getLimit() == SIZE)); stream.write(INPUT, 0, SIZE); assertTrue("Array Contents Mismatch", Arrays.equals(INPUT, stream.getBuffer())); caughtException = false; try { stream.write(INPUT[0]); } catch (Exception e) { caughtException = true; } assertTrue("Writing beyond limit did not throw an exception", caughtException); }