Base64OutputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.binary;

  18. import java.io.OutputStream;

  19. /**
  20.  * Provides Base64 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
  21.  * is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
  22.  * constructor.
  23.  * <p>
  24.  * The default behaviour of the Base64OutputStream is to ENCODE, whereas the default behaviour of the Base64InputStream
  25.  * is to DECODE. But this behaviour can be overridden by using a different constructor.
  26.  * </p>
  27.  * <p>
  28.  * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
  29.  * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
  30.  * </p>
  31.  * <p>
  32.  * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
  33.  * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
  34.  * </p>
  35.  * <p>
  36.  * <b>Note:</b> It is mandatory to close the stream after the last byte has been written to it, otherwise the
  37.  * final padding will be omitted and the resulting data will be incomplete/inconsistent.
  38.  * </p>
  39.  *
  40.  * @version $Id: Base64OutputStream.java 1635952 2014-11-01 14:19:04Z tn $
  41.  * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
  42.  * @since 1.4
  43.  */
  44. public class Base64OutputStream extends BaseNCodecOutputStream {

  45.     /**
  46.      * Creates a Base64OutputStream such that all data written is Base64-encoded to the original provided OutputStream.
  47.      *
  48.      * @param out
  49.      *            OutputStream to wrap.
  50.      */
  51.     public Base64OutputStream(final OutputStream out) {
  52.         this(out, true);
  53.     }

  54.     /**
  55.      * Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
  56.      * original provided OutputStream.
  57.      *
  58.      * @param out
  59.      *            OutputStream to wrap.
  60.      * @param doEncode
  61.      *            true if we should encode all data written to us, false if we should decode.
  62.      */
  63.     public Base64OutputStream(final OutputStream out, final boolean doEncode) {
  64.         super(out,new Base64(false), doEncode);
  65.     }

  66.     /**
  67.      * Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
  68.      * original provided OutputStream.
  69.      *
  70.      * @param out
  71.      *            OutputStream to wrap.
  72.      * @param doEncode
  73.      *            true if we should encode all data written to us, false if we should decode.
  74.      * @param lineLength
  75.      *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
  76.      *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
  77.      *            is false, lineLength is ignored.
  78.      * @param lineSeparator
  79.      *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
  80.      *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
  81.      */
  82.     public Base64OutputStream(final OutputStream out, final boolean doEncode,
  83.                               final int lineLength, final byte[] lineSeparator) {
  84.         super(out, new Base64(lineLength, lineSeparator), doEncode);
  85.     }
  86. }