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.base64;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.jboss.netty.channel.Channel;
21 import org.jboss.netty.channel.ChannelHandlerContext;
22 import org.jboss.netty.channel.ChannelPipeline;
23 import org.jboss.netty.channel.ChannelHandler.Sharable;
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.frame.FrameDecoder;
27 import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
28 import org.jboss.netty.util.CharsetUtil;
29
30 /**
31 * Decodes a Base64-encoded {@link ChannelBuffer} or US-ASCII {@link String}
32 * into a {@link ChannelBuffer}. Please note that this decoder must be used
33 * with a proper {@link FrameDecoder} such as {@link DelimiterBasedFrameDecoder}
34 * if you are using a stream-based transport such as TCP/IP. A typical decoder
35 * setup for TCP/IP would be:
36 * <pre>
37 * {@link ChannelPipeline} pipeline = ...;
38 *
39 * // Decoders
40 * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#nulDelimiter()}));
41 * pipeline.addLast("base64Decoder", new {@link Base64Decoder}());
42 *
43 * // Encoder
44 * pipeline.addLast("base64Encoder", new {@link Base64Encoder}());
45 * </pre>
46 *
47 * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
48 * @author <a href="http://gleamynode.net/">Trustin Lee</a>
49 * @version $Rev: 2241 $, $Date: 2010-04-16 13:12:43 +0900 (Fri, 16 Apr 2010) $
50 *
51 * @apiviz.landmark
52 * @apiviz.uses org.jboss.netty.handler.codec.base64.Base64
53 */
54 @Sharable
55 public class Base64Decoder extends OneToOneDecoder {
56
57 private final Base64Dialect dialect;
58
59 public Base64Decoder() {
60 this(Base64Dialect.STANDARD);
61 }
62
63 public Base64Decoder(Base64Dialect dialect) {
64 if (dialect == null) {
65 throw new NullPointerException("dialect");
66 }
67 this.dialect = dialect;
68 }
69
70 @Override
71 protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg)
72 throws Exception {
73 if (msg instanceof String) {
74 msg = ChannelBuffers.copiedBuffer((String) msg, CharsetUtil.US_ASCII);
75 } else if (!(msg instanceof ChannelBuffer)) {
76 return msg;
77 }
78
79 ChannelBuffer src = (ChannelBuffer) msg;
80 return Base64.decode(
81 src, src.readerIndex(), src.readableBytes(), dialect);
82 }
83
84 }