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.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.ChannelHandler.Sharable;
22  import org.jboss.netty.channel.ChannelHandlerContext;
23  import org.jboss.netty.channel.ChannelPipeline;
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   * @apiviz.landmark
47   * @apiviz.uses org.jboss.netty.handler.codec.base64.Base64
48   */
49  @Sharable
50  public class Base64Decoder extends OneToOneDecoder {
51  
52      private final Base64Dialect dialect;
53  
54      public Base64Decoder() {
55          this(Base64Dialect.STANDARD);
56      }
57  
58      public Base64Decoder(Base64Dialect dialect) {
59          if (dialect == null) {
60              throw new NullPointerException("dialect");
61          }
62          this.dialect = dialect;
63      }
64  
65      @Override
66      protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg)
67              throws Exception {
68          if (msg instanceof String) {
69              msg = ChannelBuffers.copiedBuffer((String) msg, CharsetUtil.US_ASCII);
70          } else if (!(msg instanceof ChannelBuffer)) {
71              return msg;
72          }
73  
74          ChannelBuffer src = (ChannelBuffer) msg;
75          return Base64.decode(
76                  src, src.readerIndex(), src.readableBytes(), dialect);
77      }
78  
79  }