diff --git a/codec/src/main/java/io/netty/handler/codec/string/StringEncoder.java b/codec/src/main/java/io/netty/handler/codec/string/StringEncoder.java index d72458cb5c..af6188f0f1 100644 --- a/codec/src/main/java/io/netty/handler/codec/string/StringEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/string/StringEncoder.java @@ -15,7 +15,6 @@ */ package io.netty.handler.codec.string; -import io.netty.buffer.BufType; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler.Sharable; @@ -50,44 +49,41 @@ import java.nio.charset.Charset; @Sharable public class StringEncoder extends ChannelOutboundMessageHandlerAdapter { - private final BufType nextBufferType; // TODO Use CharsetEncoder instead. private final Charset charset; /** * Creates a new instance with the current system character set. */ - public StringEncoder(BufType nextBufferType) { - this(nextBufferType, Charset.defaultCharset()); + public StringEncoder() { + this(Charset.defaultCharset()); } /** * Creates a new instance with the specified character set. */ - public StringEncoder(BufType nextBufferType, Charset charset) { - if (nextBufferType == null) { - throw new NullPointerException("nextBufferType"); - } + public StringEncoder(Charset charset) { if (charset == null) { throw new NullPointerException("charset"); } - this.nextBufferType = nextBufferType; this.charset = charset; } @Override public void flush(ChannelHandlerContext ctx, CharSequence msg) throws Exception { + if (msg.length() == 0) { + return; + } ByteBuf encoded = Unpooled.copiedBuffer(msg, charset); - - switch (nextBufferType) { - case BYTE: - ctx.nextOutboundByteBuffer().writeBytes(encoded); - break; - case MESSAGE: - ctx.nextOutboundMessageBuffer().add(encoded); - break; - default: - throw new Error(); + switch (ctx.nextOutboundBufferType()) { + case BYTE: + ctx.nextOutboundByteBuffer().writeBytes(encoded); + break; + case MESSAGE: + ctx.nextOutboundMessageBuffer().add(Unpooled.wrappedBuffer(encoded)); + break; + default: + throw new Error(); } } }