View Javadoc
1   /*
2    * Copyright 2016 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  import io.netty.util.CharsetUtil;
26  import io.netty.util.internal.ObjectUtil;
27  
28  import java.nio.CharBuffer;
29  import java.nio.charset.Charset;
30  import java.util.List;
31  
32  /**
33   * Apply a line separator to the requested {@link String} and encode it into a {@link ByteBuf}.
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 LineBasedFrameDecoder}(80));
40   * pipeline.addLast("stringDecoder", new {@link StringDecoder}(CharsetUtil.UTF_8));
41   *
42   * // Encoder
43   * pipeline.addLast("lineEncoder", new {@link LineEncoder}(LineSeparator.UNIX, CharsetUtil.UTF_8));
44   * </pre>
45   * and then you can use a {@link String} instead of a {@link ByteBuf}
46   * as a message:
47   * <pre>
48   * void channelRead({@link ChannelHandlerContext} ctx, {@link String} msg) {
49   *     ch.write("Did you say '" + msg + "'?");
50   * }
51   * </pre>
52   */
53  @Sharable
54  public class LineEncoder extends MessageToMessageEncoder<CharSequence> {
55  
56      private final Charset charset;
57      private final byte[] lineSeparator;
58  
59      /**
60       * Creates a new instance with the current system line separator and UTF-8 charset encoding.
61       */
62      public LineEncoder() {
63          this(LineSeparator.DEFAULT, CharsetUtil.UTF_8);
64      }
65  
66      /**
67       * Creates a new instance with the specified line separator and UTF-8 charset encoding.
68       */
69      public LineEncoder(LineSeparator lineSeparator) {
70          this(lineSeparator, CharsetUtil.UTF_8);
71      }
72  
73      /**
74       * Creates a new instance with the specified character set.
75       */
76      public LineEncoder(Charset charset) {
77          this(LineSeparator.DEFAULT, charset);
78      }
79  
80      /**
81       * Creates a new instance with the specified line separator and character set.
82       */
83      public LineEncoder(LineSeparator lineSeparator, Charset charset) {
84          this.charset = ObjectUtil.checkNotNull(charset, "charset");
85          this.lineSeparator = ObjectUtil.checkNotNull(lineSeparator, "lineSeparator").value().getBytes(charset);
86      }
87  
88      @Override
89      protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception {
90          ByteBuf buffer = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg), charset, lineSeparator.length);
91          buffer.writeBytes(lineSeparator);
92          out.add(buffer);
93      }
94  }