private void enqueue(ByteBuffer buffer) { if (buffer.isReadOnly()) { readonly = true; } if (!buffer.isDirect()) { direct = false; } if (buffer.remaining() > 0) { BufferNode newnode = new BufferNode(buffer, capacity); capacity += buffer.capacity(); if (head == null) { head = newnode; position = getPointerByPosition(0); } else { tail.setNext(newnode); } tail = newnode; limit = getPointerByPosition(capacity); } }
/** * @see ByteBuffer#slice() */ public IoBuffer slice() { position.updatePos(); IoBuffer out = new IoBuffer(); out.order(order()); position.getNode().getBuffer().position(position.getPositionInNode()); if (hasRemaining()) { tail.getBuffer().limit(limit.getPositionInNode()); for (BufferNode node = position.getNode(); node != limit.getNode(); node = node.getNext()) { if (node != head) { // NOSONAR, check if instances are the same. node.getBuffer().position(0); } out.add(node.getBuffer()); } if (tail != head) { // NOSONAR, check if instances are the same. tail.getBuffer().position(0); } out.add(tail.getBuffer().slice()); tail.getBuffer().limit(tail.getBuffer().capacity()); } position.getNode().getBuffer().position(0); return out; }
/** * @see ByteBuffer#put(byte[], int, int) */ public IoBuffer put(byte[] src, int offset, int length) { if (readonly) { throw new ReadOnlyBufferException(); } if (remaining() < length) { throw new BufferUnderflowException(); } int remainsToCopy = length; int currentOffset = offset; position.getNode().getBuffer().position(position.getPositionInNode()); while (remainsToCopy > 0) { position.updatePos(); ByteBuffer currentBuffer = position.getNode().getBuffer(); int blocksize = Math.min(remainsToCopy, currentBuffer.remaining()); position.getNode().getBuffer().put(src, currentOffset, blocksize); currentOffset += blocksize; remainsToCopy -= blocksize; position.incrementPosition(blocksize); } position.getNode().getBuffer().position(0); return this; }
/** * @see ByteBuffer#get(byte[], int,int) */ public IoBuffer get(byte[] dst, int offset, int length) { if (remaining() < length) { throw new BufferUnderflowException(); } int remainsToCopy = length; int currentOffset = offset; while (remainsToCopy > 0) { position.updatePos(); position.getNode().getBuffer().position(position.getPositionInNode()); ByteBuffer currentBuffer = position.getNode().getBuffer(); int blocksize = Math.min(remainsToCopy, currentBuffer.remaining()); position.getNode().getBuffer().get(dst, currentOffset, blocksize); currentOffset += blocksize; remainsToCopy -= blocksize; position.incrementPosition(blocksize); position.getNode().getBuffer().position(0); } return this; }
/** * Returns a copy of the current {@link IoBuffer}, with an independent copy * of the position, limit and mark. * * @return the copied {@link IoBuffer} */ public IoBuffer duplicate() { IoBuffer buffer = new IoBuffer(); for (BufferNode node = head; node != null; node = node.getNext()) { ByteBuffer byteBuffer = node.getBuffer().duplicate(); byteBuffer.rewind(); buffer.enqueue(byteBuffer); } buffer.position(position()); buffer.limit(limit()); buffer.mark = mark != null ? getPointerByPosition(mark.getPosition()) : null; buffer.readonly = readonly; return buffer; }
private IoBuffer put(Pointer pos, byte b) { pos.updatePos(); pos.getNode().getBuffer().put(pos.getPositionInNode(), b); pos.incrPosition(); return this; }
private byte get(Pointer pos) { pos.updatePos(); byte b = pos.getNode().getBuffer().get(pos.getPositionInNode()); pos.incrPosition(); return b; }