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