Search in sources :

Example 1 with RecyclableHeapByteBuf

use of com.yahoo.pulsar.common.api.Commands.RecyclableHeapByteBuf in project pulsar by yahoo.

the class ByteBufCodedInputStream method readBytes.

/** Read a {@code bytes} field value from the stream. */
public ByteString readBytes() throws IOException {
    final int size = readRawVarint32();
    if (size == 0) {
        return ByteString.EMPTY;
    } else {
        RecyclableHeapByteBuf heapBuf = RecyclableHeapByteBuf.get();
        if (size > heapBuf.writableBytes()) {
            heapBuf.capacity(size);
        }
        heapBuf.writeBytes(buf, size);
        ByteString res = ByteString.copyFrom(heapBuf.array(), heapBuf.arrayOffset(), heapBuf.readableBytes());
        heapBuf.recycle();
        return res;
    }
}
Also used : RecyclableHeapByteBuf(com.yahoo.pulsar.common.api.Commands.RecyclableHeapByteBuf) ByteString(com.google.protobuf.ByteString)

Example 2 with RecyclableHeapByteBuf

use of com.yahoo.pulsar.common.api.Commands.RecyclableHeapByteBuf in project pulsar by yahoo.

the class ByteBufCodedOutputStream method writeRawBytes.

/** Write a byte string. */
public void writeRawBytes(final ByteString value) throws IOException {
    RecyclableHeapByteBuf heapBuf = RecyclableHeapByteBuf.get();
    if (value.size() > heapBuf.writableBytes()) {
        heapBuf.capacity(value.size());
    }
    value.copyTo(heapBuf.array(), 0);
    heapBuf.writerIndex(value.size());
    buf.writeBytes(heapBuf);
    heapBuf.recycle();
}
Also used : RecyclableHeapByteBuf(com.yahoo.pulsar.common.api.Commands.RecyclableHeapByteBuf)

Aggregations

RecyclableHeapByteBuf (com.yahoo.pulsar.common.api.Commands.RecyclableHeapByteBuf)2 ByteString (com.google.protobuf.ByteString)1