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;
  }
@Test
  public void testResetBuffer() throws IOException {
    
    ResettableBoundedByteArrayOutputStream stream = 
      new ResettableBoundedByteArrayOutputStream(SIZE);

    // Write to the stream, get the data back and check for contents
    stream.write(INPUT, 0, SIZE);
    assertTrue("Array Contents Mismatch",
        Arrays.equals(INPUT, stream.getBuffer()));
    
    // Try writing beyond end of buffer. Should throw an exception
    boolean caughtException = false;
    
    try {
      stream.write(INPUT[0]);
    } catch (Exception e) {
      caughtException = true;
    }
    
    assertTrue("Writing beyond limit did not throw an exception",
        caughtException);
    
    //Reset the stream and try, should succeed
    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()));
  
    // Try writing one more byte, should fail
    caughtException = false;
    try {
      stream.write(INPUT[0]);
    } catch (Exception e) {
      caughtException = true;
    }
    assertTrue("Writing beyond limit did not throw an exception",
        caughtException);
  }