private IoBuffer() { limit(0); position(0); mark = null; }
/** * @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(); }
/** * @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); }
/** * 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 over a buffer */ @Test public void testPositionBuffer() { 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); // Iterate and check the position for (int i = 0; i < ioBuffer.limit(); i++) { assertEquals(i, ioBuffer.position()); ioBuffer.get(); } }
@Test public void testGet() { 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); ioBuffer.position(2); ioBuffer.limit(8); byte block[] = new byte[6]; ioBuffer.get(block); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], block[i]); } }
/** * Test the clear() method */ @Test public void testClearEmptyBuffer() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("345".getBytes()); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); assertEquals(6, ioBuffer.limit()); // Move forward a bit ioBuffer.get(); ioBuffer.get(); ioBuffer.limit(3); // Clear ioBuffer.clear(); // We should be back to the origin assertEquals(0, ioBuffer.position()); // The limit must back to the available size assertEquals(6, ioBuffer.limit()); }
@Test public void testCompact() { 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); ioBuffer.position(2); ioBuffer.limit(8); ioBuffer.compact(); assertEquals(ioBuffer.capacity(), ioBuffer.limit()); assertEquals(6, ioBuffer.position()); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], ioBuffer.get(i)); } }
/** * Test the flip() method */ @Test public void testFlip() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("0123".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("4567".getBytes()); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); // Move forward a bit ioBuffer.get(); ioBuffer.get(); // Clear ioBuffer.clear(); // We should be back to the origin assertEquals(0, ioBuffer.position()); assertEquals(8, ioBuffer.limit()); }
@Test public void testSlice() { 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); ioBuffer.position(2); ioBuffer.limit(8); IoBuffer slice = ioBuffer.slice(); assertEquals(6, slice.remaining()); assertEquals(0, slice.position()); assertEquals(6, slice.limit()); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], slice.get(i)); } }
@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 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"); } } } }
/** * Test the get() method on a IoBuffer containing one ByteBuffer with 0 * bytes */ @Test public void testGetOneBuffer0Bytes() { ByteBuffer bb = ByteBuffer.allocate(0); IoBuffer ioBuffer = IoBuffer.wrap(bb); assertEquals(0, ioBuffer.position()); assertEquals(0, ioBuffer.limit()); try { assertFalse(ioBuffer.hasRemaining()); ioBuffer.get(); fail(); } catch (BufferUnderflowException bufe) { // expected assertEquals(0, ioBuffer.position()); } }
@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 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()); }
/** * 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()); }