// Create a safe duplicate of the message to write it to a channel but not affect other writes. // See https://github.com/netty/netty/issues/1461 private static Object safeDuplicate(Object message) { if (message instanceof ByteBuf) { return ((ByteBuf) message).duplicate().retain(); } else if (message instanceof ByteBufHolder) { return ((ByteBufHolder) message).duplicate().retain(); } else { return ReferenceCountUtil.retain(message); } }
@Override public ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher) { if (message == null) { throw new NullPointerException("message"); } Map<Channel, ChannelFuture> futures = new LinkedHashMap<Channel, ChannelFuture>(size()); for (Channel c : nonServerChannels) { if (matcher.matches(c)) { futures.put(c, c.writeAndFlush(safeDuplicate(message))); } } ReferenceCountUtil.release(message); return new DefaultChannelGroupFuture(this, futures, executor); }