Exemplo n.º 1
0
  /**
   * Writes all the remaining bytes of the {@code src} byte buffer to this buffer's current
   * position, and increases both buffers' position by the number of bytes copied.
   *
   * @param src the source byte buffer.
   * @return {@code this}
   * @throws BufferOverflowException if {@code src.remaining()} is greater than this buffer's {@code
   *     remaining()}.
   * @throws IllegalArgumentException if {@code src} is this buffer.
   * @throws ReadOnlyBufferException if no changes may be made to the contents of this buffer.
   */
  public ByteBuffer put(ByteBuffer src) {
    if (isReadOnly()) {
      throw new ReadOnlyBufferException();
    }
    if (src == this) {
      throw new IllegalArgumentException("src == this");
    }
    int srcByteCount = src.remaining();
    if (srcByteCount > remaining()) {
      throw new BufferOverflowException();
    }

    Object srcObject = src.isDirect() ? src : NioUtils.unsafeArray(src);
    int srcOffset = src.position();
    if (!src.isDirect()) {
      srcOffset += NioUtils.unsafeArrayOffset(src);
    }

    ByteBuffer dst = this;
    Object dstObject = dst.isDirect() ? dst : NioUtils.unsafeArray(dst);
    int dstOffset = dst.position();
    if (!dst.isDirect()) {
      dstOffset += NioUtils.unsafeArrayOffset(dst);
    }

    Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, srcByteCount);
    src.position(src.limit());
    dst.position(dst.position() + srcByteCount);

    return this;
  }
Exemplo n.º 2
0
 @Override
 public ByteBuffer compact() {
   if (isReadOnly) {
     throw new ReadOnlyBufferException();
   }
   Memory.memmove(this, 0, this, position, remaining());
   position = limit - position;
   limit = capacity;
   mark = UNSET_MARK;
   return this;
 }