/** * @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; }
/** * Test the getInt() method, on a buffer containing 2 ints in two * ByteBuffers with LittleInidan order */ @Test public void testGetInt2Ints2BBsLittleIndian() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.order(ByteOrder.LITTLE_ENDIAN); bb1.putInt(12345); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.order(ByteOrder.LITTLE_ENDIAN); bb2.putInt(67890); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); ioBuffer.order(ByteOrder.LITTLE_ENDIAN); assertEquals(12345, ioBuffer.getInt()); assertEquals(67890, ioBuffer.getInt()); }
@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 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); } }
for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putChar('\u00EB').rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); ioBuffer.order(bo); ioBuffer.position(2); assertEquals(4, ioBuffer.capacity());
@Test public void testShort() { for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putShort((short) 12345).rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); assertEquals(3, ioBuffer.capacity()); ioBuffer.extend(1); ioBuffer.position(2); assertEquals(4, ioBuffer.capacity()); ioBuffer.putShort((short) -23456); ioBuffer.rewind(); assertEquals(12345, ioBuffer.getShort()); assertEquals(-23456, ioBuffer.getShort()); ioBuffer.rewind(); ioBuffer.putShort(1, (short) 12345); assertEquals((short) 12345, ioBuffer.getShort(1)); try { ioBuffer.putShort(3, (short) 1); fail("Not enough place on the buffer"); } catch (BufferUnderflowException e) { // Should come here } try { ioBuffer.getShort(3); fail("Not enough place on the buffer"); } catch (BufferUnderflowException e) { // Should come here } } }
@Test public void testInt() { for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putInt(123456).rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); assertEquals(5, ioBuffer.capacity()); ioBuffer.extend(3); ioBuffer.position(4); assertEquals(8, ioBuffer.capacity()); ioBuffer.putInt(-23456789); ioBuffer.rewind(); assertEquals(123456, ioBuffer.getInt()); assertEquals(-23456789, ioBuffer.getInt()); ioBuffer.rewind(); ioBuffer.putInt(2, 1234567890); assertEquals(1234567890, ioBuffer.getInt(2)); try { ioBuffer.putInt(5, 1); fail("Not enough place on the buffer"); } catch (BufferUnderflowException e) { // Should come here } try { ioBuffer.getInt(5); fail("Not enough place on the buffer"); } catch (BufferUnderflowException e) { // Should come here } } }
@Test public void testLong() { for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) { ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putLong(123456789012l).rewind(); IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo); assertEquals(9, ioBuffer.capacity()); ioBuffer.extend(7); ioBuffer.position(8); assertEquals(16, ioBuffer.capacity()); ioBuffer.putLong(-23456789023l); ioBuffer.rewind(); assertEquals(123456789012l, ioBuffer.getLong()); assertEquals(-23456789023l, ioBuffer.getLong()); ioBuffer.rewind(); ioBuffer.putLong(4, 1234567890); assertEquals(1234567890, ioBuffer.getLong(4)); try { ioBuffer.putLong(9, 1); fail("Not enough place on the buffer"); } catch (BufferUnderflowException e) { // Should come here } try { ioBuffer.getLong(9); fail("Not enough place on the buffer"); } catch (BufferUnderflowException e) { // Should come here } } }