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 org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.channel.Channel;
20  import org.jboss.netty.channel.ChannelHandler.Sharable;
21  import org.jboss.netty.channel.ChannelHandlerContext;
22  import org.jboss.netty.channel.ChannelPipeline;
23  import org.jboss.netty.channel.MessageEvent;
24  import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
25  import org.jboss.netty.handler.codec.frame.Delimiters;
26  import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
27  
28  import java.nio.charset.Charset;
29  
30  import static org.jboss.netty.buffer.ChannelBuffers.*;
31  
32  /**
33   * Encodes the requested {@link String} into a {@link ChannelBuffer}.
34   * A typical setup for a 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 DelimiterBasedFrameDecoder}({@link Delimiters#lineDelimiter()}));
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 ChannelBuffer}
46   * as a message:
47   * <pre>
48   * void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
49   *     String msg = (String) e.getMessage();
50   *     ch.write("Did you say '" + msg + "'?\n");
51   * }
52   * </pre>
53   *
54   * @apiviz.landmark
55   */
56  @Sharable
57  public class StringEncoder extends OneToOneEncoder {
58  
59      // TODO Use CharsetEncoder instead.
60      private final Charset charset;
61  
62      /**
63       * Creates a new instance with the current system character set.
64       */
65      public StringEncoder() {
66          this(Charset.defaultCharset());
67      }
68  
69      /**
70       * Creates a new instance with the specified character set.
71       */
72      public StringEncoder(Charset charset) {
73          if (charset == null) {
74              throw new NullPointerException("charset");
75          }
76          this.charset = charset;
77      }
78  
79      @Override
80      protected Object encode(
81              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
82          if (msg instanceof String) {
83              return copiedBuffer(
84                      ctx.getChannel().getConfig().getBufferFactory().getDefaultOrder(), (String) msg, charset);
85          }
86  
87          return msg;
88      }
89  }