private IoBuffer() { limit(0); position(0); mark = null; }
/** * @see ByteBuffer#rewind() */ public IoBuffer rewind() { position(0); mark = getPointerByPosition(-1); return this; }
/** * @see ByteBuffer#remaining() */ public int remaining() { return limit() - position(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getName()); sb.append("[pos="); sb.append(position()); sb.append(" lim="); sb.append(limit()); sb.append(" cap="); sb.append(capacity()); sb.append("]"); return sb.toString(); }
/** * 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; }
/** * Test the position method with a negative value */ @Test(expected = IllegalArgumentException.class) public void testPositionNegativeValue() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("0123".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("4567".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(4); bb3.put("89".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3); ioBuffer.position(-1); }
/** * Test the position method with a value above the buffer size */ @Test(expected = IllegalArgumentException.class) public void testPositionAboveValue() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("3456".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(4); bb3.put("789".getBytes()); bb3.flip(); // The resulting buffer will be seen as "0123456789" IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3); ioBuffer.position(11); }
/** * Test the position method over an emptyIoBuffer */ @Test public void testPositionIntEmptyBuffer() { IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.position(0); }
@Test public void testInputStreamGetByte() throws IOException { String hw = "HelloWorld"; IoBuffer bb = IoBuffer.wrap(hw.getBytes()); InputStream is = bb.asInputStream(); for (int i = 0; i < 10; i++) { assertEquals(i, bb.position()); assertEquals(hw.getBytes()[i], is.read()); } assertEquals(-1, is.read()); }
@Test public void testInputStreamGetByteArray() throws IOException { String hw = "HelloWorld"; IoBuffer bb = IoBuffer.wrap(hw.getBytes()); InputStream is = bb.asInputStream(); byte array[] = new byte[15]; assertEquals(5, is.read(array, 0, 5)); assertEquals(5, bb.position()); assertEquals(5, is.read(array, 5, 10)); assertEquals(10, bb.position()); for (int i = 0; i < 10; i++) { assertEquals(hw.getBytes()[i], array[i]); } }
/** * Test the position method over an emptyIoBuffer */ @Test public void testPositionEmptyBuffer() { IoBuffer ioBuffer = IoBuffer.newInstance(); assertEquals(0, ioBuffer.position()); }
@Override public OUT decode(IoBuffer input, MutableInt nextBlockSize) { OUT output = null; if (nextBlockSize.getValue() == null) { nextBlockSize.setValue(sizeDecoder.decode(input)); } if (nextBlockSize.isDefined() && (input.remaining() >= nextBlockSize.getValue())) { IoBuffer buffer = input.slice(); buffer.limit(buffer.position() + nextBlockSize.getValue()); output = payloadDecoder.decode(buffer); nextBlockSize.reset(); } return output; }
@Test public void testFloat() { for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putFloat(-0.68f).rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); assertEquals(5, ioBuffer.capacity()); ioBuffer.extend(3); ioBuffer.position(4); assertEquals(8, ioBuffer.capacity()); ioBuffer.putFloat(3.14f); ioBuffer.rewind(); assertEquals(-0.68f, ioBuffer.getFloat(), 0.001f); assertEquals(3.14f, ioBuffer.getFloat(), 0.001f); ioBuffer.rewind(); ioBuffer.putFloat(2, -12.34f); assertEquals(-12.34f, ioBuffer.getFloat(2), 0.001f); } }
@Test public void testDouble() { for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putDouble(Math.PI).rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); assertEquals(9, ioBuffer.capacity()); ioBuffer.extend(7); ioBuffer.position(8); assertEquals(16, ioBuffer.capacity()); ioBuffer.putDouble(-Math.E); ioBuffer.rewind(); assertEquals(Math.PI, ioBuffer.getDouble(), 1E-10); assertEquals(-Math.E, ioBuffer.getDouble(), 1E-10); ioBuffer.rewind(); ioBuffer.putDouble(4, 12.34); assertEquals(12.34, ioBuffer.getDouble(4), 1E-10); } }
@Test public void testTruncatedValues() { for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) { IoBuffer buffer = IoBuffer.wrap(encoder.encode(value)); for (int i = 0; i < buffer.remaining(); i++) { IoBuffer partialBuffer = buffer.slice(); partialBuffer.limit(partialBuffer.position() + i); try { assertNull(decoder.decode(partialBuffer)); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } } }
/** * Test the allocation of a new heap IoBuffer with 1024 bytes */ @Test public void testAllocate1024() { IoBuffer ioBuffer = IoBuffer.allocate(1024); assertFalse(ioBuffer.isDirect()); assertEquals(1024, ioBuffer.capacity()); assertEquals(1024, ioBuffer.limit()); assertEquals(0, ioBuffer.position()); assertTrue(ioBuffer.hasRemaining()); }
/** * Test the allocation of a new heap IoBuffer with no byte in it */ @Test public void testAllocate0() { IoBuffer ioBuffer = IoBuffer.allocate(0); assertFalse(ioBuffer.isDirect()); assertEquals(0, ioBuffer.capacity()); assertEquals(0, ioBuffer.limit()); assertEquals(0, ioBuffer.position()); assertFalse(ioBuffer.hasRemaining()); }
/** * Test the allocation of a new direct IoBuffer with 1024 bytes */ @Test public void testAllocateDirect1024() { IoBuffer ioBuffer = IoBuffer.allocateDirect(1024); assertTrue(ioBuffer.isDirect()); assertEquals(1024, ioBuffer.capacity()); assertEquals(1024, ioBuffer.limit()); assertEquals(0, ioBuffer.position()); assertTrue(ioBuffer.hasRemaining()); }
/** * Test the allocation of a new direct IoBuffer with no byte in it */ @Test public void testAllocateDirect0() { IoBuffer ioBuffer = IoBuffer.allocateDirect(0); assertTrue(ioBuffer.isDirect()); assertEquals(0, ioBuffer.capacity()); assertEquals(0, ioBuffer.limit()); assertEquals(0, ioBuffer.position()); assertFalse(ioBuffer.hasRemaining()); }