View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.string;
17  
18  import java.nio.charset.Charset;
19  
20  import org.jboss.netty.buffer.ChannelBuffer;
21  import org.jboss.netty.channel.Channel;
22  import org.jboss.netty.channel.ChannelHandlerContext;
23  import org.jboss.netty.channel.ChannelPipeline;
24  import org.jboss.netty.channel.MessageEvent;
25  import org.jboss.netty.channel.ChannelHandler.Sharable;
26  import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
27  import org.jboss.netty.handler.codec.frame.Delimiters;
28  import org.jboss.netty.handler.codec.frame.FrameDecoder;
29  import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
30  
31  /**
32   * Decodes a received {@link ChannelBuffer} into a {@link String}.  Please
33   * note that this decoder must be used with a proper {@link FrameDecoder}
34   * such as {@link DelimiterBasedFrameDecoder} if you are using a stream-based
35   * transport such as TCP/IP.  A typical setup for a text-based line protocol
36   * in a TCP/IP socket would be:
37   * <pre>
38   * {@link ChannelPipeline} pipeline = ...;
39   *
40   * // Decoders
41   * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#lineDelimiter()}));
42   * pipeline.addLast("stringDecoder", new {@link StringDecoder}(CharsetUtil.UTF_8));
43   *
44   * // Encoder
45   * pipeline.addLast("stringEncoder", new {@link StringEncoder}(CharsetUtil.UTF_8));
46   * </pre>
47   * and then you can use a {@link String} instead of a {@link ChannelBuffer}
48   * as a message:
49   * <pre>
50   * void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
51   *     String msg = (String) e.getMessage();
52   *     ch.write("Did you say '" + msg + "'?\n");
53   * }
54   * </pre>
55   *
56   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
57   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
58   *
59   * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $
60   *
61   * @apiviz.landmark
62   */
63  @Sharable
64  public class StringDecoder extends OneToOneDecoder {
65  
66      // TODO Use CharsetDecoder instead.
67      private final Charset charset;
68  
69      /**
70       * Creates a new instance with the current system character set.
71       */
72      public StringDecoder() {
73          this(Charset.defaultCharset());
74      }
75  
76      /**
77       * Creates a new instance with the specified character set.
78       */
79      public StringDecoder(Charset charset) {
80          if (charset == null) {
81              throw new NullPointerException("charset");
82          }
83          this.charset = charset;
84      }
85  
86      /**
87       * @deprecated Use {@link #StringDecoder(Charset)} instead.
88       */
89      @Deprecated
90      public StringDecoder(String charsetName) {
91          this(Charset.forName(charsetName));
92      }
93  
94      @Override
95      protected Object decode(
96              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
97          if (!(msg instanceof ChannelBuffer)) {
98              return msg;
99          }
100         return ((ChannelBuffer) msg).toString(charset);
101     }
102 }