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;
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<CharSequence> {
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();
}
}
}