View Javadoc

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