Added JavaDoc for handler.codec.string

This commit is contained in:
Trustin Lee 2008-09-04 03:14:19 +00:00
parent 26a1d6e429
commit 49e62cd000
2 changed files with 67 additions and 2 deletions

View File

@ -29,30 +29,70 @@ import java.nio.charset.Charset;
import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelEvent; import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineCoverage; import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
/** /**
* Decodes a received {@link ChannelBuffer} into a {@link String}. Please
* note that this decoder must be used with a proper {@link FrameDecoder}
* such as {@link DelimiterBasedFrameDecoder} if you are using a stream-based
* transport such as TCP/IP. A typical decoder setup for a text-based line
* protocol in a TCP/IP socket would be:
* <pre>
* {@link ChannelPipeline} pipeline = ...;
*
* // Decoders
* pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}({@link Delimiters#lineDelimiter()}));
* pipeline.addLast("stringDecoder", new {@link StringDecoder}("UTF-8"));
*
* // Encoder
* pipeline.addLast("stringEncoder", new {@link StringEncoder}("UTF-8"));
* </pre>
* and then you can use {@link String}s instead of {@link ChannelBuffer}s
* as a message:
* <pre>
* void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
* String msg = (String) e.getMessage();
* ch.write("Did you say '" + msg + "'?\n");
* }
* </pre>
*
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
* *
* @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (, 12 6월 2008) $ * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (, 12 6월 2008) $
*
*/ */
@ChannelPipelineCoverage("all") @ChannelPipelineCoverage("all")
public class StringDecoder implements ChannelUpstreamHandler { public class StringDecoder implements ChannelUpstreamHandler {
private final String charsetName; private final String charsetName;
/**
* Creates a new instance with the current system character set.
*/
public StringDecoder() { public StringDecoder() {
this(Charset.defaultCharset()); this(Charset.defaultCharset());
} }
/**
* Creates a new instance.
*
* @param charsetName the name of the character set to use for decoding
*/
public StringDecoder(String charsetName) { public StringDecoder(String charsetName) {
this(Charset.forName(charsetName)); this(Charset.forName(charsetName));
} }
/**
* Creates a new instance.
*
* @param charset the character set to use for decoding
*/
public StringDecoder(Charset charset) { public StringDecoder(Charset charset) {
if (charset == null) { if (charset == null) {
throw new NullPointerException("charset"); throw new NullPointerException("charset");

View File

@ -27,18 +27,43 @@ import static org.jboss.netty.channel.Channels.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelDownstreamHandler; import org.jboss.netty.channel.ChannelDownstreamHandler;
import org.jboss.netty.channel.ChannelEvent; import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineCoverage; import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
/** /**
* Encodes the requested {@link String} into a {@link ChannelBuffer}.
* The typical decoder setup for a text-based line protocol in a TCP/IP socket
* would be:
* <pre>
* {@link ChannelPipeline} pipeline = ...;
*
* // Decoders
* pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}({@link Delimiters#lineDelimiter()}));
* pipeline.addLast("stringDecoder", new {@link StringDecoder}("UTF-8"));
*
* // Encoder
* pipeline.addLast("stringEncoder", new {@link StringEncoder}("UTF-8"));
* </pre>
* and then you can use {@link String}s instead of {@link ChannelBuffer}s
* as a message:
* <pre>
* void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
* String msg = (String) e.getMessage();
* ch.write("Did you say '" + msg + "'?\n");
* }
* </pre>
*
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
* *
* @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (, 12 6월 2008) $ * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (, 12 6월 2008) $
*
*/ */
@ChannelPipelineCoverage("all") @ChannelPipelineCoverage("all")
public class StringEncoder implements ChannelDownstreamHandler { public class StringEncoder implements ChannelDownstreamHandler {