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