Example usage for io.netty.buffer ByteBuf setBytes

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

Introduction

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

Prototype

public abstract int setBytes(int index, ScatteringByteChannel in, int length) throws IOException;

Source Link

Document

Transfers the content of the specified source channel to this buffer starting at the specified absolute index .

Usage

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

License:Apache License

@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
    checkIndex(index, length);//from www  . j  av  a2 s.com
    if (length == 0) {
        return in.read(EmptyArrays.EMPTY_BYTES);
    }

    int i = findIndex(index);
    int readBytes = 0;

    do {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.writableBytes());
        int localReadBytes = s.setBytes(index - adjustment, in, localLength);
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        }

        if (localReadBytes == localLength) {
            index += localLength;
            length -= localLength;
            readBytes += localLength;
            i++;
        } else {
            index += localReadBytes;
            length -= localReadBytes;
            readBytes += localReadBytes;
        }
    } while (length > 0);

    return readBytes;
}

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

License:Apache License

@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);/*  w w w . j  a v  a  2s .  c om*/
    if (length == 0) {
        return in.read(FULL_BYTEBUFFER);
    }

    int i = findIndex(index);
    int readBytes = 0;
    do {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.writableBytes());
        int localReadBytes = s.setBytes(index - adjustment, in, localLength);

        if (localReadBytes == 0) {
            break;
        }

        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        }

        if (localReadBytes == localLength) {
            index += localLength;
            length -= localLength;
            readBytes += localLength;
            i++;
        } else {
            index += localReadBytes;
            length -= localReadBytes;
            readBytes += localReadBytes;
        }
    } while (length > 0);

    return readBytes;
}