/** {@inheritDoc} */
  public void insert(int offset, byte[] values, int valOffset, int len) {
    if (offset == _pos) {
      add(values, valOffset, len);
      return;
    }

    ensureCapacity(_pos + len);
    // shift right
    System.arraycopy(_data, offset, _data, offset + len, _pos - offset);
    // insert
    System.arraycopy(values, valOffset, _data, offset, len);
    _pos += len;
  }
 /**
  * Grow the internal array as needed to accommodate the specified number of elements. The size of
  * the array bytes on each resize unless capacity requires more than twice the current capacity.
  */
 public void ensureCapacity(int capacity) {
   if (capacity > _data.length) {
     int newCap = Math.max(_data.length << 1, capacity);
     byte[] tmp = new byte[newCap];
     System.arraycopy(_data, 0, tmp, 0, _data.length);
     _data = tmp;
   }
 }
 /** {@inheritDoc} */
 public byte[] toArray(byte[] dest, int source_pos, int dest_pos, int len) {
   if (len == 0) {
     return dest; // nothing to copy
   }
   if (source_pos < 0 || source_pos >= _pos) {
     throw new ArrayIndexOutOfBoundsException(source_pos);
   }
   System.arraycopy(_data, source_pos, dest, dest_pos, len);
   return dest;
 }
 /** {@inheritDoc} */
 public byte[] toArray(byte[] dest, int offset, int len) {
   if (len == 0) {
     return dest; // nothing to copy
   }
   if (offset < 0 || offset >= _pos) {
     throw new ArrayIndexOutOfBoundsException(offset);
   }
   System.arraycopy(_data, offset, dest, 0, len);
   return dest;
 }
  /** {@inheritDoc} */
  public void remove(int offset, int length) {
    if (length == 0) return;
    if (offset < 0 || offset >= _pos) {
      throw new ArrayIndexOutOfBoundsException(offset);
    }

    if (offset == 0) {
      // data at the front
      System.arraycopy(_data, length, _data, 0, _pos - length);
    } else if (_pos - length == offset) {
      // no copy to make, decrementing pos "deletes" values at
      // the end
    } else {
      // data in the middle
      System.arraycopy(_data, offset + length, _data, offset, _pos - (offset + length));
    }
    _pos -= length;
    // no need to clear old values beyond _pos, because this is a
    // primitive collection and 0 takes as much room as any other
    // value
  }
 /** {@inheritDoc} */
 public void insert(int offset, byte value) {
   if (offset == _pos) {
     add(value);
     return;
   }
   ensureCapacity(_pos + 1);
   // shift right
   System.arraycopy(_data, offset, _data, offset + 1, _pos - offset);
   // insert
   _data[offset] = value;
   _pos++;
 }
 /** {@inheritDoc} */
 public void set(int offset, byte[] values, int valOffset, int length) {
   if (offset < 0 || offset + length > _pos) {
     throw new ArrayIndexOutOfBoundsException(offset);
   }
   System.arraycopy(values, valOffset, _data, offset, length);
 }
 /** {@inheritDoc} */
 public void add(byte[] vals, int offset, int length) {
   ensureCapacity(_pos + length);
   System.arraycopy(vals, offset, _data, _pos, length);
   _pos += length;
 }