[#1729] Let StringEncoder extend MessageToMessageEncoder and so safe a memory copy

This commit is contained in:
Norman Maurer 2013-08-11 21:24:02 +02:00
parent 9a88c50ffb
commit 73755d1233

View File

@ -21,9 +21,10 @@ import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import java.nio.charset.Charset;
import java.util.List;
/**
* Encodes the requested {@link String} into a {@link ByteBuf}.
@ -47,7 +48,7 @@ import java.nio.charset.Charset;
* </pre>
*/
@Sharable
public class StringEncoder extends MessageToByteEncoder<CharSequence> {
public class StringEncoder extends MessageToMessageEncoder<CharSequence> {
// TODO Use CharsetEncoder instead.
private final Charset charset;
@ -70,16 +71,11 @@ public class StringEncoder extends MessageToByteEncoder<CharSequence> {
}
@Override
protected void encode(ChannelHandlerContext ctx, CharSequence msg, ByteBuf out) throws Exception {
protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception {
if (msg.length() == 0) {
return;
}
ByteBuf encoded = Unpooled.copiedBuffer(msg, charset);
try {
out.writeBytes(encoded);
} finally {
encoded.release();
}
out.add(Unpooled.copiedBuffer(msg, charset));
}
}