IoBuffer { 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) { node.getBuffer().position(0); } out.add(node.getBuffer()); } if (tail != head) { tail.getBuffer().position(0); } out.add(tail.getBuffer().slice()); tail.getBuffer().limit(tail.getBuffer().capacity()); } position.getNode().getBuffer().position(0); return out; } private  IoBuffer(); static IoBuffer allocate(int capacity); static IoBuffer allocateDirect(int capacity); static IoBuffer newInstance(); static IoBuffer wrap(byte[]... arrays); static IoBuffer wrap(byte[] array, int offset, int length); static IoBuffer wrap(ByteBuffer... buffers); IoBuffer add(ByteBuffer... buffers); byte[] array(); int arrayOffset(); InputStream asInputStream(); IoBuffer asReadOnlyBuffer(); int capacity(); IoBuffer clear(); IoBuffer compact(); IoBuffer duplicate(); @Override boolean equals(Object ob); IoBuffer extend(int size); IoBuffer flip(); byte get(); IoBuffer get(byte[] dst); IoBuffer get(byte[] dst, int offset, int length); byte get(int index); char getChar(); char getChar(int index); double getDouble(); double getDouble(int index); float getFloat(); float getFloat(int index); int getInt(); int getInt(int index); long getLong(); long getLong(int index); short getShort(); long getShort(int index); @Override int hashCode(); boolean hasRemaining(); boolean isDirect(); boolean isReadOnly(); int limit(); void limit(int newLimit); void mark(); ByteOrder order(); IoBuffer order(ByteOrder bo); int position(); void position(int newPosition); IoBuffer put(byte b); IoBuffer put(byte[] src); IoBuffer put(ByteBuffer src); IoBuffer put(IoBuffer src); IoBuffer put(byte[] src, int offset, int length); IoBuffer put(int index, byte value); IoBuffer putChar(char value); IoBuffer putChar(int index, char value); IoBuffer putDouble(double value); IoBuffer putDouble(int index, double value); IoBuffer putFloat(float value); IoBuffer putFloat(int index, float value); IoBuffer putInt(int value); IoBuffer putInt(int index, int value); IoBuffer putLong(int index, long value); IoBuffer putLong(long value); IoBuffer putShort(int index, short value); IoBuffer putShort(short value); int remaining(); IoBuffer reset(); IoBuffer rewind(); IoBuffer slice(); @Override String toString();  }
@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)); } }