Search in sources :

Example 1 with ReadableBuffer

use of io.grpc.internal.ReadableBuffer in project alluxio by Alluxio.

the class DataMessageMarshaller method parse.

@Override
public T parse(InputStream message) {
    ReadableBuffer rawBuffer = GrpcSerializationUtils.getBufferFromStream(message);
    try {
        if (rawBuffer != null) {
            CompositeReadableBuffer readableBuffer = new CompositeReadableBuffer();
            readableBuffer.addBuffer(rawBuffer);
            return deserialize(readableBuffer);
        } else {
            // falls back to buffer copy
            byte[] byteBuffer = new byte[message.available()];
            message.read(byteBuffer);
            return deserialize(ReadableBuffers.wrap(byteBuffer));
        }
    } catch (Throwable t) {
        if (rawBuffer != null) {
            rawBuffer.close();
        }
        throw new RuntimeException(t);
    }
}
Also used : CompositeReadableBuffer(io.grpc.internal.CompositeReadableBuffer) CompositeReadableBuffer(io.grpc.internal.CompositeReadableBuffer) ReadableBuffer(io.grpc.internal.ReadableBuffer)

Example 2 with ReadableBuffer

use of io.grpc.internal.ReadableBuffer in project alluxio by Alluxio.

the class GrpcSerializationUtils method getByteBufFromReadableBuffer.

/**
 * Gets a Netty buffer directly from a gRPC ReadableBuffer.
 *
 * @param buffer the input buffer
 * @return the raw ByteBuf, or null if the ByteBuf cannot be extracted
 */
public static ByteBuf getByteBufFromReadableBuffer(ReadableBuffer buffer) {
    if (!sZeroCopyReceiveSupported) {
        return null;
    }
    try {
        if (buffer instanceof CompositeReadableBuffer) {
            Queue<ReadableBuffer> buffers = (Queue<ReadableBuffer>) sCompositeBuffers.get(buffer);
            if (buffers.size() == 1) {
                return getByteBufFromReadableBuffer(buffers.peek());
            } else {
                CompositeByteBuf buf = PooledByteBufAllocator.DEFAULT.compositeBuffer();
                for (ReadableBuffer readableBuffer : buffers) {
                    ByteBuf subBuffer = getByteBufFromReadableBuffer(readableBuffer);
                    if (subBuffer == null) {
                        return null;
                    }
                    buf.addComponent(true, subBuffer);
                }
                return buf;
            }
        } else if (buffer.getClass().equals(sReadableByteBuf.getDeclaringClass())) {
            return (ByteBuf) sReadableByteBuf.get(buffer);
        }
    } catch (Exception e) {
        LOG.warn("Failed to get data buffer from stream: {}.", e.toString());
        return null;
    }
    return null;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeReadableBuffer(io.grpc.internal.CompositeReadableBuffer) CompositeReadableBuffer(io.grpc.internal.CompositeReadableBuffer) ReadableBuffer(io.grpc.internal.ReadableBuffer) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) Queue(java.util.Queue)

Aggregations

CompositeReadableBuffer (io.grpc.internal.CompositeReadableBuffer)2 ReadableBuffer (io.grpc.internal.ReadableBuffer)2 ByteBuf (io.netty.buffer.ByteBuf)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 Queue (java.util.Queue)1