Example usage for io.netty.buffer ByteBuf writableBytes

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

Introduction

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

Prototype

public abstract int writableBytes();

Source Link

Document

Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex) .

Usage

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

License:Apache License

@Override
public AlternativeCompositeByteBuf getBytes(int index, ByteBuf dst) {
    getBytes(index, dst, dst.writableBytes());
    return this;
}

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

License:Apache License

@Override
public AlternativeCompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
    checkSrcIndex(index, length, srcIndex, src.capacity());
    if (length == 0) {
        return this;
    }/*from   w  w w.j  av  a2  s .  c  om*/

    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.writableBytes());
        s.setBytes(index - adjustment, src, srcIndex, localLength);
        index += localLength;
        srcIndex += localLength;
        length -= localLength;
        i++;
    }
    return this;
}

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

License:Apache License

@Override
public AlternativeCompositeByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
    checkSrcIndex(index, length, srcIndex, src.length);
    if (length == 0) {
        return this;
    }/*www  .ja v  a  2s .  c o m*/

    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.writableBytes());
        s.setBytes(index - adjustment, src, srcIndex, localLength);
        index += localLength;
        srcIndex += localLength;
        length -= localLength;
        i++;
    }
    return this;
}

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

License:Apache License

@Override
public AlternativeCompositeByteBuf setBytes(int index, ByteBuffer src) {
    int limit = src.limit();
    int length = src.remaining();

    checkIndex(index, length);//from   w ww . j av a2s .  c o m
    if (length == 0) {
        return this;
    }

    int i = findIndex(index);
    try {
        while (length > 0) {
            Component c = components.get(i);
            ByteBuf s = c.buf;
            int adjustment = c.offset;
            int localLength = Math.min(length, s.writableBytes());
            src.limit(src.position() + localLength);
            s.setBytes(index - adjustment, src);
            index += localLength;
            length -= localLength;
            i++;
        }
    } finally {
        src.limit(limit);
    }
    return this;
}

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);//w ww  . j ava2  s. c  o m
    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);//from w ww  .ja  v a 2s.  co m
    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;
}

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

License:Apache License

@Override
public ByteBuf readBytes(ByteBuf dst) {
    readBytes(dst, dst.writableBytes());
    return this;
}

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

License:Apache License

@Override
public ByteBuf readBytes(ByteBuf dst, int length) {
    if (length > dst.writableBytes()) {
        throw new IndexOutOfBoundsException(String.format(
                "length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
    }//w  w  w .  j a  va 2s.co m
    readBytes(dst, dst.writerIndex(), length);
    dst.writerIndex(dst.writerIndex() + length);
    return this;
}

From source file:openbns.commons.net.codec.sts.HttpObjectEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
    ByteBuf buf = null;

    if (msg instanceof StsMessage) {
        if (state != ST_INIT) {
            throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
        }/*from   ww  w .ja  v a2 s.  co m*/

        @SuppressWarnings({ "unchecked", "CastConflictsWithInstanceof" })
        H m = (H) msg;

        buf = ctx.alloc().buffer();
        // Encode the message.
        encodeInitialLine(buf, m);
        StsHeaders.encode(m.headers(), buf);
        buf.writeBytes(CRLF);
        state = ST_CONTENT_NON_CHUNK;
    }
    if (msg instanceof StsContent || msg instanceof ByteBuf || msg instanceof FileRegion) {
        if (state == ST_INIT) {
            throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
        }

        int contentLength = contentLength(msg);
        if (state == ST_CONTENT_NON_CHUNK) {
            if (contentLength > 0) {
                if (buf != null && buf.writableBytes() >= contentLength && msg instanceof StsContent) {
                    // merge into other buffer for performance reasons
                    buf.writeBytes(((StsContent) msg).content());
                    out.add(buf);
                } else {
                    if (buf != null) {
                        out.add(buf);
                    }
                    out.add(encodeAndRetain(msg));
                }
            } else {
                if (buf != null) {
                    out.add(buf);
                } else {
                    // Need to produce some output otherwise an
                    // IllegalStateException will be thrown
                    out.add(EMPTY_BUFFER);
                }
            }

            if (msg instanceof LastStsContent) {
                state = ST_INIT;
            }
        } else {
            throw new Error();
        }
    } else {
        if (buf != null) {
            out.add(buf);
        }
    }
}

From source file:org.apache.activemq.artemis.protocol.amqp.util.DeliveryUtil.java

License:Apache License

public static int readDelivery(Receiver receiver, ByteBuf buffer) {
    int initial = buffer.writerIndex();
    // optimization by norman
    int count;//from w  ww  .ja v a 2s  .  c o m
    while ((count = receiver.recv(buffer.array(), buffer.arrayOffset() + buffer.writerIndex(),
            buffer.writableBytes())) > 0) {
        // Increment the writer index by the number of bytes written into it while calling recv.
        buffer.writerIndex(buffer.writerIndex() + count);
        buffer.ensureWritable(count);
    }
    return buffer.writerIndex() - initial;
}