use of io.netty.channel.FileRegion in project alluxio by Alluxio.
the class RPCMessageEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, RPCMessage in, List<Object> out) throws Exception {
RPCRequest.Type type = in.getType();
long bodyBytes = 0;
DataBuffer payload = null;
if (in.hasPayload()) {
payload = in.getPayloadDataBuffer();
bodyBytes = payload.getLength();
}
int lengthBytes = Longs.BYTES;
int typeBytes = type.getEncodedLength();
int messageBytes = in.getEncodedLength();
int headerBytes = lengthBytes + typeBytes + messageBytes;
long frameBytes = headerBytes + bodyBytes;
// Write the header info into a buffer.
// The format is: [frame length][message type][message][(optional) data]
ByteBuf buffer = ctx.alloc().buffer();
buffer.writeLong(frameBytes);
type.encode(buffer);
in.encode(buffer);
// Output the header buffer.
out.add(buffer);
if (payload != null && bodyBytes > 0) {
Object output = payload.getNettyOutput();
Preconditions.checkArgument(output instanceof ByteBuf || output instanceof FileRegion, "The payload must be a ByteBuf or a FileRegion.");
out.add(output);
}
}
use of io.netty.channel.FileRegion in project reactor-netty by reactor.
the class ChannelOperationsHandler method doWrite.
ChannelFuture doWrite(Object msg, ChannelPromise promise, PublisherSender inner) {
if (// fastpath
flushOnEach || // last drained element
inner == null && pendingWrites.isEmpty() || !ctx.channel().isWritable()) {
pendingBytes = 0L;
ChannelFuture future = ctx.write(msg, promise);
if (flushOnEachWithEventLoop && ctx.channel().isWritable()) {
scheduleFlush();
} else {
ctx.flush();
}
return future;
} else {
if (msg instanceof ByteBuf) {
pendingBytes = Operators.addCap(pendingBytes, ((ByteBuf) msg).readableBytes());
} else if (msg instanceof ByteBufHolder) {
pendingBytes = Operators.addCap(pendingBytes, ((ByteBufHolder) msg).content().readableBytes());
} else if (msg instanceof FileRegion) {
pendingBytes = Operators.addCap(pendingBytes, ((FileRegion) msg).count());
}
if (log.isTraceEnabled()) {
log.trace("{} Pending write size = {}", ctx.channel(), pendingBytes);
}
ChannelFuture future = ctx.write(msg, promise);
if (!ctx.channel().isWritable()) {
pendingBytes = 0L;
ctx.flush();
}
return future;
}
}
use of io.netty.channel.FileRegion in project reactor-netty by reactor.
the class NettyOutboundTest method sendFileWithoutTlsUsesFileRegion.
@Test
public void sendFileWithoutTlsUsesFileRegion() throws URISyntaxException {
List<Class<?>> messageClasses = new ArrayList<>(2);
EmbeddedChannel channel = new EmbeddedChannel(new MessageToMessageEncoder<FileRegion>() {
@Override
protected void encode(ChannelHandlerContext ctx, FileRegion msg, List<Object> out) throws Exception {
ByteArrayOutputStream bais = new ByteArrayOutputStream();
WritableByteChannel wbc = Channels.newChannel(bais);
msg.transferTo(wbc, msg.position());
out.add(new String(bais.toByteArray(), StandardCharsets.UTF_8));
}
}, new MessageToMessageEncoder<Object>() {
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
messageClasses.add(msg.getClass());
ReferenceCountUtil.retain(msg);
out.add(msg);
}
});
NettyContext mockContext = () -> channel;
NettyOutbound outbound = new NettyOutbound() {
@Override
public NettyContext context() {
return mockContext;
}
@Override
public FileChunkedStrategy getFileChunkedStrategy() {
return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
}
};
channel.writeOneOutbound(1);
outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then().block();
assertThat(channel.inboundMessages()).isEmpty();
assertThat(channel.outboundMessages()).hasSize(2);
assertThat(messageClasses).containsExactly(Integer.class, DefaultFileRegion.class);
assertThat(channel.outboundMessages()).element(1).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("GARBAGE End of File");
assertThat(channel.finishAndReleaseAll()).isTrue();
}
use of io.netty.channel.FileRegion in project rocketmq by apache.
the class QueryMessageProcessor method viewMessageById.
public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
final ViewMessageRequestHeader requestHeader = (ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);
response.setOpaque(request.getOpaque());
final SelectMappedBufferResult selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
if (selectMappedBufferResult != null) {
response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
try {
FileRegion fileRegion = new OneMessageTransfer(response.encodeHeader(selectMappedBufferResult.getSize()), selectMappedBufferResult);
ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
selectMappedBufferResult.release();
if (!future.isSuccess()) {
log.error("Transfer one message from page cache failed, ", future.cause());
}
}
});
} catch (Throwable e) {
log.error("", e);
selectMappedBufferResult.release();
}
return null;
} else {
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
}
return response;
}
use of io.netty.channel.FileRegion in project rocketmq by apache.
the class FileRegionEncoderTest method testEncode.
/**
* This unit test case ensures that {@link FileRegionEncoder} indeed wraps {@link FileRegion} to
* {@link ByteBuf}.
* @throws IOException if there is an error.
*/
@Test
public void testEncode() throws IOException {
FileRegionEncoder fileRegionEncoder = new FileRegionEncoder();
EmbeddedChannel channel = new EmbeddedChannel(fileRegionEncoder);
File file = File.createTempFile(UUID.randomUUID().toString(), ".data");
file.deleteOnExit();
Random random = new Random(System.currentTimeMillis());
int dataLength = 1 << 10;
byte[] data = new byte[dataLength];
random.nextBytes(data);
write(file, data);
FileRegion fileRegion = new DefaultFileRegion(file, 0, dataLength);
Assert.assertEquals(0, fileRegion.transfered());
Assert.assertEquals(dataLength, fileRegion.count());
Assert.assertTrue(channel.writeOutbound(fileRegion));
ByteBuf out = (ByteBuf) channel.readOutbound();
byte[] arr = new byte[out.readableBytes()];
out.getBytes(0, arr);
Assert.assertArrayEquals("Data should be identical", data, arr);
}
Aggregations