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