在调用ChannelOutboundInvoker.flush()
或ChannelOutboundInvoker.writeAndFlush()
之后直接重用ByteBuf(包装的字节数组)是否安全?
byte[] bytes = ...
ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
ctx.writeAndFlush(byteBuf);
bytes[0] = 0; // safe to do here?
我已经尝试了数十次,每次都很好,但是我想知道是否保证可以。
答案 0 :(得分:1)
不,这不安全/不正确,因为不能保证立即执行。唯一“安全”的方法是在byte[]
返回的ChannelFuture
完成后重新使用writeAndFlush(...)
。
类似这样的东西:
byte[] bytes = ...
ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
ctx.writeAndFlush(byteBuf).addListener(f -> {
bytes[0] = 0;
});