带FileRegion的Netty 4 MessageToByteEncoder

时间:2014-08-18 23:55:02

标签: netty

我正在从原始NIO迁移到netty。我需要回复的回复如下

short
long
long
long
file

我有以下工作示例,并想知道如何将FileRegion移动到编码器中。

MessageToByteEncoder

@Override
protected void encode(final ChannelHandlerContext ctx, final BlockResponse msg,
    final ByteBuf out) throws Exception {
  out.writeShort(DataServerMessage.DATA_SERVER_RESPONSE_MESSAGE);
  out.writeLong(msg.getBlockId());
  out.writeLong(msg.getOffset());
  out.writeLong(msg.getLength());
}

ChannelInboundHandlerAdapter

    ctx.write(new BlockResponse(blockId, offset, readLength));
    FileChannel channel = closer.register(file.getChannel());
    ChannelFuture future = ctx.writeAndFlush(new DefaultFileRegion(channel, offset, readLength));
    future.addListener(ChannelFutureListener.CLOSE);

我认为如果我在适配器中将writeAndFlush写入响应(并将文件放在那里),那么我可以在编码器中执行另一个writeAndFlush,但编码器需要关闭它。还有另一种方式吗?

谢谢!

编辑:

以下是适用的更新代码:

public static final class Encoder extends MessageToMessageEncoder<BlockResponse> {
  private static final int HEADER_LENGTH = 2 + 4 * 3; // short, 3 longs

  @Override
  protected void encode(final ChannelHandlerContext ctx, final BlockResponse msg,
      final List<Object> out) throws Exception {
    out.add(createHeader(ctx, msg));
    if (msg.getChannel() != null) {
      out.add(new DefaultFileRegion(msg.getChannel(), msg.getOffset(), msg.getLength()));
    }
  }

  private ByteBuf createHeader(final ChannelHandlerContext ctx, final BlockResponse msg) {
    ByteBuf header = ctx.alloc().buffer(HEADER_LENGTH);
    header.writeShort(DataServerMessage.DATA_SERVER_RESPONSE_MESSAGE);
    header.writeLong(msg.getBlockId());
    header.writeLong(msg.getOffset());
    header.writeLong(msg.getLength());
    return header;
  }
}

ChannelInboundHandlerAdapter

ChannelFuture future =
    ctx.writeAndFlush(new BlockResponse(blockId, offset, readLength, channel));
future.addListener(ChannelFutureListener.CLOSE);
future.addListener(new ClosableResourceChannelListener(file));

1 个答案:

答案 0 :(得分:1)

如果您还需要在编码器中触发FileRegion,则需要使用MessageToMessageEncoder并在其中分配ByteBuf。