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);
}
}
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;
}
Aggregations