@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
ChannelBufferOutputStream bout =
new ChannelBufferOutputStream(dynamicBuffer(
estimatedLength, ctx.getChannel().getConfig().getBufferFactory()));
bout.write(LENGTH_PLACEHOLDER);
ObjectOutputStream oout = new CompactObjectOutputStream(bout);
oout.writeObject(msg);
oout.flush();
oout.close();
ChannelBuffer encoded = bout.buffer();
encoded.setInt(0, encoded.writerIndex() - 4);
return encoded;
}
本文介绍了一种使用Netty框架进行对象序列化的编码方法。该方法通过ChannelBufferOutputStream和CompactObjectOutputStream将对象写入缓冲区,并预留长度占位符以便后续更新实际长度。完成对象写入后,通过设置预留位置的值来填充实际的数据长度。
586

被折叠的 条评论
为什么被折叠?



