Example usage for io.netty.buffer ByteBuf nioBuffers

List of usage examples for io.netty.buffer ByteBuf nioBuffers

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf nioBuffers.

Prototype

public abstract ByteBuffer[] nioBuffers(int index, int length);

Source Link

Document

Exposes this buffer's bytes as an NIO ByteBuffer 's for the specified index and length The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer.

Usage

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

@Override
public ByteBuffer[] nioBuffers(int index, int length) {
    checkIndex(index, length);//from   w  w  w.jav a 2s.c om
    if (length == 0) {
        return EmptyArrays.EMPTY_BYTE_BUFFERS;
    }

    List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(components.size());
    int i = findIndex(index);
    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.readableBytes() - (index - adjustment));
        switch (s.nioBufferCount()) {
        case 0:
            throw new UnsupportedOperationException();
        case 1:
            buffers.add(s.nioBuffer(index - adjustment, localLength));
            break;
        default:
            Collections.addAll(buffers, s.nioBuffers(index - adjustment, localLength));
        }

        index += localLength;
        length -= localLength;
        i++;
    }

    return buffers.toArray(new ByteBuffer[buffers.size()]);
}