@Test public void testSlice() { CompositeReadableBuffer buffer = new CompositeReadableBuffer(); byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; buffer.append(data); assertEquals(buffer.capacity(), data.length); buffer.position(1); buffer.limit(buffer.capacity() - 2); ReadableBuffer slice = buffer.slice(); assertEquals(slice.position(), 0); assertEquals(slice.limit(), buffer.remaining()); assertEquals(slice.capacity(), buffer.remaining()); assertEquals(1, slice.get()); try { slice.reset(); fail("Should throw InvalidMarkException"); } catch (InvalidMarkException e) { } }
@Override public CompositeReadableBuffer slice() { int newCapacity = limit() - position(); final CompositeReadableBuffer result; if (newCapacity == 0) { result = EMPTY_SLICE; } else { result = new CompositeReadableBuffer(currentArray, currentOffset); result.contents = contents; result.currentArrayIndex = currentArrayIndex; result.capacity = newCapacity; result.limit = newCapacity; result.position = 0; result.compactable = false; } return result; }
CompositeReadableBuffer implements ReadableBuffer { @Override public CompositeReadableBuffer slice() { int newCapacity = limit() - position(); final CompositeReadableBuffer result; if (newCapacity == 0) { result = EMPTY_SLICE; } else { result = new CompositeReadableBuffer(currentArray, currentOffset); result.contents = contents; result.currentArrayIndex = currentArrayIndex; result.capacity = newCapacity; result.limit = newCapacity; result.position = 0; result.compactable = false; } return result; } }
CompositeReadableBuffer implements ReadableBuffer { @Override public CompositeReadableBuffer slice() { int newCapacity = limit() - position(); final CompositeReadableBuffer result; if (newCapacity == 0) { result = EMPTY_SLICE; } else { result = new CompositeReadableBuffer(currentArray, currentOffset); result.contents = contents; result.currentArrayIndex = currentArrayIndex; result.capacity = newCapacity; result.limit = newCapacity; result.position = 0; result.compactable = false; } return result; } CompositeReadableBuffer(); private  CompositeReadableBuffer(byte[] array, int offset); private  CompositeReadableBuffer(boolean compactable); }
CompositeReadableBuffer implements ReadableBuffer { @Override public CompositeReadableBuffer slice() { int newCapacity = limit() - position(); final CompositeReadableBuffer result; if (newCapacity == 0) { result = EMPTY_SLICE; } else { result = new CompositeReadableBuffer(currentArray, currentOffset); result.contents = contents; result.currentArrayIndex = currentArrayIndex; result.capacity = newCapacity; result.limit = newCapacity; result.position = 0; result.compactable = false; } return result; } CompositeReadableBuffer(); private  CompositeReadableBuffer(byte[] array, int offset); private  CompositeReadableBuffer(boolean compactable); List<byte[]> getArrays(); int getCurrentIndex(); int getCurrentArrayPosition(); @Override boolean hasArray(); @Override int capacity(); @Override byte[] array(); @Override int arrayOffset(); @Override byte get(); @Override byte get(int index); @Override int getInt(); @Override long getLong(); @Override short getShort(); @Override float getFloat(); @Override double getDouble(); @Override CompositeReadableBuffer get(byte[] data); @Override CompositeReadableBuffer get(byte[] data, int offset, int length); @Override CompositeReadableBuffer get(WritableBuffer target); @Override CompositeReadableBuffer position(int position); @Override int position(); @Override CompositeReadableBuffer slice(); @Override CompositeReadableBuffer flip(); @Override CompositeReadableBuffer limit(int limit); @Override int limit(); @Override CompositeReadableBuffer mark(); @Override CompositeReadableBuffer reset(); @Override CompositeReadableBuffer rewind(); @Override CompositeReadableBuffer clear(); @Override int remaining(); @Override boolean hasRemaining(); @Override CompositeReadableBuffer duplicate(); @Override ByteBuffer byteBuffer(); @Override String readUTF8(); @Override String readString(CharsetDecoder decoder); @Override CompositeReadableBuffer reclaimRead(); CompositeReadableBuffer append(byte[] array); CompositeReadableBuffer append(CompositeReadableBuffer buffer); CompositeReadableBuffer append(ReadableBuffer buffer); @Override int hashCode(); @Override boolean equals(Object other); @Override String toString(); }
CompositeReadableBuffer implements ReadableBuffer { @Override public CompositeReadableBuffer slice() { int newCapacity = limit() - position(); final CompositeReadableBuffer result; if (newCapacity == 0) { result = EMPTY_SLICE; } else { result = new CompositeReadableBuffer(currentArray, currentOffset); result.contents = contents; result.currentArrayIndex = currentArrayIndex; result.capacity = newCapacity; result.limit = newCapacity; result.position = 0; result.compactable = false; } return result; } CompositeReadableBuffer(); private  CompositeReadableBuffer(byte[] array, int offset); private  CompositeReadableBuffer(boolean compactable); List<byte[]> getArrays(); int getCurrentIndex(); int getCurrentArrayPosition(); @Override boolean hasArray(); @Override int capacity(); @Override byte[] array(); @Override int arrayOffset(); @Override byte get(); @Override byte get(int index); @Override int getInt(); @Override long getLong(); @Override short getShort(); @Override float getFloat(); @Override double getDouble(); @Override CompositeReadableBuffer get(byte[] data); @Override CompositeReadableBuffer get(byte[] data, int offset, int length); @Override CompositeReadableBuffer get(WritableBuffer target); @Override CompositeReadableBuffer position(int position); @Override int position(); @Override CompositeReadableBuffer slice(); @Override CompositeReadableBuffer flip(); @Override CompositeReadableBuffer limit(int limit); @Override int limit(); @Override CompositeReadableBuffer mark(); @Override CompositeReadableBuffer reset(); @Override CompositeReadableBuffer rewind(); @Override CompositeReadableBuffer clear(); @Override int remaining(); @Override boolean hasRemaining(); @Override CompositeReadableBuffer duplicate(); @Override ByteBuffer byteBuffer(); @Override String readUTF8(); @Override String readString(CharsetDecoder decoder); @Override CompositeReadableBuffer reclaimRead(); CompositeReadableBuffer append(byte[] array); CompositeReadableBuffer append(CompositeReadableBuffer buffer); CompositeReadableBuffer append(ReadableBuffer buffer); @Override int hashCode(); @Override boolean equals(Object other); @Override String toString();  }