code: public void toArray(int offset, int length, byte[] targetBuffer, int targetOffset) {
  
          // validate the offset, length are ok
          ByteBuffer.checkOffsetLength(size(), offset, length);
  
          // validate the offset, length are ok
          ByteBuffer.checkOffsetLength(targetBuffer.length, targetOffset, length);
  
          // will we have a large enough byte[] allocated?
          if (targetBuffer.length < length) {
              throw new IllegalArgumentException("TargetBuffer size must be large enough to hold a byte[] of at least a size=" + length);
          }
  
          // are we actually copying any data?
          if (length > 0) {
  
              // create adjusted versions of read and write positions based
              // on the offset and length passed into this method
              int offsetReadPosition = (this.currentReadPosition + offset) % this.buffer.length;
              int offsetWritePosition = (this.currentReadPosition + offset + length) % this.buffer.length;
  
              if (offsetReadPosition >= offsetWritePosition) {
                  System.arraycopy(
                      this.buffer,
                      offsetReadPosition,
                      targetBuffer,
                      targetOffset,
                      this.buffer.length - offsetReadPosition);
                  System.arraycopy(
                      this.buffer,
                      0,
                      targetBuffer,
                      targetOffset + this.buffer.length - offsetReadPosition,
                      offsetWritePosition);
              } else {
                  System.arraycopy(
                      this.buffer,
                      offsetReadPosition,
                      targetBuffer,
                      targetOffset,
                      offsetWritePosition - offsetReadPosition);
              }
          }
      }
docs: Will copy data from this ByteBuffer's buffer into the targetBuffer.  This
  method requires the targetBuffer to already be allocated with enough capacity
  to hold this ByteBuffer's data.
  @param offset The offset within the ByteBuffer to start copy from
  @param length The length from the offset to copy
  @param targetBuffer The target byte array we'll copy data into. Must already
  be allocated with enough capacity.
  @param targetOffset The offset within the target byte array to start from
  @throws IllegalArgumentException If the offset and length are invalid
  for this ByteBuffer, if the targetOffset and targetLength are invalid
  for the targetBuffer, or if if the targetBuffer's capacity is not
  large enough to hold the copied data.