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.  *      https://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. import org.apache.commons.codec.CodecPolicy;

  20. /**
  21.  * Provides Base64 encoding in a streaming fashion (unlimited size). When encoding the default lineLength
  22.  * is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
  23.  * constructor.
  24.  * <p>
  25.  * The default behavior of the Base64OutputStream is to ENCODE, whereas the default behavior of the Base64InputStream
  26.  * is to DECODE. But this behavior can be overridden by using a different constructor.
  27.  * </p>
  28.  * <p>
  29.  * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
  30.  * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
  31.  * </p>
  32.  * <p>
  33.  * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
  34.  * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
  35.  * </p>
  36.  * <p>
  37.  * <strong>Note:</strong> It is mandatory to close the stream after the last byte has been written to it, otherwise the
  38.  * final padding will be omitted and the resulting data will be incomplete/inconsistent.
  39.  * </p>
  40.  * <p>
  41.  * You can set the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a
  42.  * valid encoding. These can be bits that are unused from the final character or entire characters. The default mode is
  43.  * lenient decoding.
  44.  * </p>
  45.  * <ul>
  46.  * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.
  47.  * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid
  48.  * encoding. Any unused bits from the final character must be zero. Impossible counts of entire final characters are not
  49.  * allowed.
  50.  * </ul>
  51.  * <p>
  52.  * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches
  53.  * the original, i.e. no changes occur on the final character. This requires that the input bytes use the same padding
  54.  * and alphabet as the encoder.
  55.  * </p>
  56.  * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
  57.  * @since 1.4
  58.  */
  59. public class Base64OutputStream extends BaseNCodecOutputStream {

  60.     /**
  61.      * Constructs a Base64OutputStream such that all data written is Base64-encoded to the original provided OutputStream.
  62.      *
  63.      * @param outputStream
  64.      *            OutputStream to wrap.
  65.      */
  66.     public Base64OutputStream(final OutputStream outputStream) {
  67.         this(outputStream, true);
  68.     }

  69.     /**
  70.      * Constructs a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
  71.      * original provided OutputStream.
  72.      *
  73.      * @param outputStream
  74.      *            OutputStream to wrap.
  75.      * @param doEncode
  76.      *            true if we should encode all data written to us, false if we should decode.
  77.      */
  78.     public Base64OutputStream(final OutputStream outputStream, final boolean doEncode) {
  79.         super(outputStream, new Base64(false), doEncode);
  80.     }

  81.     /**
  82.      * Constructs a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
  83.      * original provided OutputStream.
  84.      *
  85.      * @param outputStream
  86.      *            OutputStream to wrap.
  87.      * @param doEncode
  88.      *            true if we should encode all data written to us, false if we should decode.
  89.      * @param lineLength
  90.      *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
  91.      *            the nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If
  92.      *            doEncode is false, lineLength is ignored.
  93.      * @param lineSeparator
  94.      *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (for example \r\n).
  95.      *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
  96.      */
  97.     public Base64OutputStream(final OutputStream outputStream, final boolean doEncode, final int lineLength, final byte[] lineSeparator) {
  98.         super(outputStream, new Base64(lineLength, lineSeparator), doEncode);
  99.     }

  100.     /**
  101.      * Constructs a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
  102.      * original provided OutputStream.
  103.      *
  104.      * @param outputStream
  105.      *            OutputStream to wrap.
  106.      * @param doEncode
  107.      *            true if we should encode all data written to us, false if we should decode.
  108.      * @param lineLength
  109.      *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
  110.      *            the nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If
  111.      *            doEncode is false, lineLength is ignored.
  112.      * @param lineSeparator
  113.      *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (for example \r\n).
  114.      *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
  115.      * @param decodingPolicy The decoding policy.
  116.      * @since 1.15
  117.      */
  118.     public Base64OutputStream(final OutputStream outputStream, final boolean doEncode, final int lineLength, final byte[] lineSeparator,
  119.         final CodecPolicy decodingPolicy) {
  120.         super(outputStream, new Base64(lineLength, lineSeparator, false, decodingPolicy), doEncode);
  121.     }
  122. }