No need to pass the next BufType to the constructor

This commit is contained in:
Norman Maurer 2013-05-06 07:46:26 +02:00
parent c230afaed1
commit 108c7d9b44

View File

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