View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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.ChannelHandler.Sharable;
23  import org.jboss.netty.channel.ChannelHandlerContext;
24  import org.jboss.netty.channel.ChannelPipeline;
25  import org.jboss.netty.channel.MessageEvent;
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   * @apiviz.landmark
57   */
58  @Sharable
59  public class StringDecoder extends OneToOneDecoder {
60  
61      // TODO Use CharsetDecoder instead.
62      private final Charset charset;
63  
64      /**
65       * Creates a new instance with the current system character set.
66       */
67      public StringDecoder() {
68          this(Charset.defaultCharset());
69      }
70  
71      /**
72       * Creates a new instance with the specified character set.
73       */
74      public StringDecoder(Charset charset) {
75          if (charset == null) {
76              throw new NullPointerException("charset");
77          }
78          this.charset = charset;
79      }
80  
81      /**
82       * @deprecated Use {@link #StringDecoder(Charset)} instead.
83       */
84      @Deprecated
85      public StringDecoder(String charsetName) {
86          this(Charset.forName(charsetName));
87      }
88  
89      @Override
90      protected Object decode(
91              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
92          if (!(msg instanceof ChannelBuffer)) {
93              return msg;
94          }
95          return ((ChannelBuffer) msg).toString(charset);
96      }
97  }