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 io.netty.handler.codec.string;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandler.Sharable;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelPipeline;
22  import io.netty.handler.codec.ByteToMessageDecoder;
23  import io.netty.handler.codec.DelimiterBasedFrameDecoder;
24  import io.netty.handler.codec.LineBasedFrameDecoder;
25  import io.netty.handler.codec.MessageToMessageDecoder;
26  
27  import java.nio.charset.Charset;
28  import java.util.List;
29  
30  /**
31   * Decodes a received {@link ByteBuf} into a {@link String}.  Please
32   * note that this decoder must be used with a proper {@link ByteToMessageDecoder}
33   * such as {@link DelimiterBasedFrameDecoder} or {@link LineBasedFrameDecoder}
34   * if you are using a stream-based transport such as TCP/IP.  A typical setup for a
35   * text-based line protocol in a TCP/IP socket would be:
36   * <pre>
37   * {@link ChannelPipeline} pipeline = ...;
38   *
39   * // Decoders
40   * pipeline.addLast("frameDecoder", new {@link LineBasedFrameDecoder}(80));
41   * pipeline.addLast("stringDecoder", new {@link StringDecoder}(CharsetUtil.UTF_8));
42   *
43   * // Encoder
44   * pipeline.addLast("stringEncoder", new {@link StringEncoder}(CharsetUtil.UTF_8));
45   * </pre>
46   * and then you can use a {@link String} instead of a {@link ByteBuf}
47   * as a message:
48   * <pre>
49   * void channelRead({@link ChannelHandlerContext} ctx, {@link String} msg) {
50   *     ch.write("Did you say '" + msg + "'?\n");
51   * }
52   * </pre>
53   */
54  @Sharable
55  public class StringDecoder extends MessageToMessageDecoder<ByteBuf> {
56  
57      // TODO Use CharsetDecoder instead.
58      private final Charset charset;
59  
60      /**
61       * Creates a new instance with the current system character set.
62       */
63      public StringDecoder() {
64          this(Charset.defaultCharset());
65      }
66  
67      /**
68       * Creates a new instance with the specified character set.
69       */
70      public StringDecoder(Charset charset) {
71          if (charset == null) {
72              throw new NullPointerException("charset");
73          }
74          this.charset = charset;
75      }
76  
77      @Override
78      protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
79          out.add(msg.toString(charset));
80      }
81  }