1 /*
2 * Copyright 2009 Red Hat, Inc.
3 *
4 * Red Hat licenses this file to you under the Apache License, version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * 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 static org.jboss.netty.buffer.ChannelBuffers.*;
19
20 import java.nio.charset.Charset;
21
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.jboss.netty.channel.Channel;
24 import org.jboss.netty.channel.ChannelHandlerContext;
25 import org.jboss.netty.channel.ChannelPipeline;
26 import org.jboss.netty.channel.MessageEvent;
27 import org.jboss.netty.channel.ChannelHandler.Sharable;
28 import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
29 import org.jboss.netty.handler.codec.frame.Delimiters;
30 import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
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 * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
55 * @author <a href="http://gleamynode.net/">Trustin Lee</a>
56 *
57 * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $
58 *
59 * @apiviz.landmark
60 */
61 @Sharable
62 public class StringEncoder extends OneToOneEncoder {
63
64 // TODO Use CharsetEncoder instead.
65 private final Charset charset;
66
67 /**
68 * Creates a new instance with the current system character set.
69 */
70 public StringEncoder() {
71 this(Charset.defaultCharset());
72 }
73
74 /**
75 * Creates a new instance with the specified character set.
76 */
77 public StringEncoder(Charset charset) {
78 if (charset == null) {
79 throw new NullPointerException("charset");
80 }
81 this.charset = charset;
82 }
83
84 /**
85 * @deprecated Use {@link #StringEncoder(Charset)} instead.
86 */
87 @Deprecated
88 public StringEncoder(String charsetName) {
89 this(Charset.forName(charsetName));
90 }
91
92 @Override
93 protected Object encode(
94 ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
95 if (!(msg instanceof String)) {
96 return msg;
97 }
98 return copiedBuffer((String) msg, charset);
99 }
100 }