/** * @see ByteBuffer#put(byte[]) */ public IoBuffer put(byte[] src) { put(src, 0, src.length); return this; }
/** * @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; }
/** * @see ByteBuffer#put(ByteBuffer) */ public IoBuffer put(ByteBuffer src) { if (remaining() < src.remaining()) { throw new BufferOverflowException(); } if (isReadOnly()) { throw new ReadOnlyBufferException(); } while (src.hasRemaining()) { put(src.get()); } 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 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; }
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 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; }
/** * @see ByteBuffer#put(ByteBuffer) */ public IoBuffer put(IoBuffer src) { if (src == this) { // NOSONAR, checking the instance throw new IllegalArgumentException(); } if (remaining() < src.remaining()) { throw new BufferOverflowException(); } if (isReadOnly()) { throw new ReadOnlyBufferException(); } while (src.hasRemaining()) { put(src.get()); } return this; }
@Test public void testPut() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(5); bb2.put("345".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("6789".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); byte seq[] = "abcdefghij".getBytes(); ioBuffer.position(2); ioBuffer.put(seq, 3, 3); ioBuffer.rewind(); byte expected[] = "01def56789".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(expected[i], ioBuffer.get(i)); } }
/** * @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; }
@Test public void testExtendedValues() { for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) { ByteBuffer buffer = encoder.encode(value); for (int i = 1; i < 5; i++) { int size = buffer.remaining() + i; IoBuffer extendedBuffer = IoBuffer.wrap(ByteBuffer.allocate(size)); int start = extendedBuffer.position(); extendedBuffer.put(buffer.slice()); extendedBuffer.position(start); extendedBuffer.limit(start + size); try { decoder.decode(extendedBuffer); assertEquals(i, extendedBuffer.remaining()); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } } }