/** * @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; }
/** * 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; }
/** * @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; }
/** * {@inheritDoc} */ @Override public int hashCode() { int hash = 0; Pointer oldPos = position.duplicate(); while (hasRemaining()) { hash *= 31; // NOSONAR, standard way of hashing hash += get(); } position = oldPos; return hash; }
public Pointer duplicate() { return new Pointer(getPosition()); }
public int getPositionInNode() { updatePos(); return positionInBuffer; }
public Pointer(int position) { this(); setPosition(position); }
private Pointer getPointerByPosition(int pos) { return new Pointer(pos); }
private IoBuffer putLong(Pointer pointer, long value) { if (position.getPosition() > pointer.getPosition() || pointer.getPosition() > limit.getPosition() - Long.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } for (int i = 0; i < Long.SIZE; i += Byte.SIZE) { put(pointer, (byte) (value >> (bo == ByteOrder.BIG_ENDIAN ? (Long.SIZE - Byte.SIZE) - i : i))); } return this; }
private IoBuffer putInt(Pointer pointer, int value) { if (position.getPosition() > pointer.getPosition() || pointer.getPosition() > limit.getPosition() - Integer.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } for (int i = 0; i < Integer.SIZE; i += Byte.SIZE) { put(pointer, (byte) (value >> (bo == ByteOrder.BIG_ENDIAN ? (Integer.SIZE - Byte.SIZE) - i : i))); } return this; }
private long getLong(Pointer pos) { if (pos.getPosition() > capacity - Long.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } long out = 0; for (int i = 0; i < Long.SIZE; i += Byte.SIZE) { out |= (get(pos) & BYTE_MASK_L) << (bo == ByteOrder.BIG_ENDIAN ? (Long.SIZE - Byte.SIZE) - i : i); } return out; }
private int getInt(Pointer pos) { if (pos.getPosition() > capacity - Integer.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } int out = 0; for (int i = 0; i < Integer.SIZE; i += Byte.SIZE) { out |= (get(pos) & BYTE_MASK) << (bo == ByteOrder.BIG_ENDIAN ? (Integer.SIZE - Byte.SIZE) - i : i); } return out; }
/** * @see ByteBuffer#get(int) */ public byte get(int index) { if (index >= limit.getPosition()) { throw new IndexOutOfBoundsException(); } return get(getPointerByPosition(index)); }
/** * @see ByteBuffer#compact() */ public IoBuffer compact() { for (int i = 0; i < remaining(); i++) { put(i, get(i + position.getPosition())); } position(limit() - position()); limit(capacity); mark = null; return this; }
/** * @see ByteBuffer#reset() */ public IoBuffer reset() { if (mark == null) { throw new InvalidMarkException(); } position = mark.duplicate(); return this; }
/** * @see ByteBuffer#position() */ public int position() { return position.getPosition(); }
public void incrPosition() { incrementPosition(1); }
/** * @see ByteBuffer#put(byte) */ public IoBuffer put(byte b) { if (readonly) { throw new ReadOnlyBufferException(); } if (position.getPosition() >= limit.getPosition()) { throw new BufferUnderflowException(); } put(position, b); return this; }