@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getName()); sb.append("[pos="); sb.append(getPosition()); sb.append(", node="); sb.append(getNode()); sb.append("]"); return sb.toString(); }
/** * @see ByteBuffer#position(int) */ public void position(int newPosition) { if (newPosition > limit() || newPosition < 0) { throw new IllegalArgumentException(); } if (mark != null && mark.getPosition() > newPosition) { mark = null; } this.position.setPosition(newPosition); }
/** * @see ByteBuffer#put(int, byte) */ public IoBuffer put(int index, byte value) { if (index >= limit.getPosition()) { throw new IndexOutOfBoundsException(); } Pointer p = getPointerByPosition(index); put(p, value); 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; }
public Pointer duplicate() { return new Pointer(getPosition()); }
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#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; }
private short getShort(Pointer pos) { if (pos.getPosition() > capacity - Short.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } if (bo == ByteOrder.BIG_ENDIAN) { return (short) ((get(pos) & BYTE_MASK) << Byte.SIZE | (get(pos) & BYTE_MASK)); } else { return (short) ((get(pos) & BYTE_MASK) | (get(pos) & BYTE_MASK) << Byte.SIZE); } }
/** * @see ByteBuffer#position() */ public int position() { return position.getPosition(); }
/** * @see ByteBuffer#limit() */ public int limit() { return limit.getPosition(); }
private IoBuffer putShort(Pointer pointer, short value) { if (position.getPosition() > pointer.getPosition() || pointer.getPosition() > limit.getPosition() - Short.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } for (int i = 0; i < Short.SIZE; i += Byte.SIZE) { put(pointer, (byte) (value >> (bo == ByteOrder.BIG_ENDIAN ? Byte.SIZE - i : i))); } return this; }