Base32OutputStream.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 Base32 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 Base32OutputStream is to ENCODE, whereas the default behavior of the Base32InputStream
  26.  * is to DECODE. But this behavior can be overridden by using a different constructor.
  27.  * </p>
  28.  * <p>
  29.  * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
  30.  * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
  31.  * </p>
  32.  * <p>
  33.  * <strong>Note:</strong> It is mandatory to close the stream after the last byte has been written to it, otherwise the
  34.  * final padding will be omitted and the resulting data will be incomplete/inconsistent.
  35.  * </p>
  36.  * <p>
  37.  * You can set the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a
  38.  * valid encoding. These can be bits that are unused from the final character or entire characters. The default mode is
  39.  * lenient decoding.
  40.  * </p>
  41.  * <ul>
  42.  * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.
  43.  * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid
  44.  * encoding. Any unused bits from the final character must be zero. Impossible counts of entire final characters are not
  45.  * allowed.
  46.  * </ul>
  47.  * <p>
  48.  * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches
  49.  * the original, i.e. no changes occur on the final character. This requires that the input bytes use the same padding
  50.  * and alphabet as the encoder.
  51.  * </p>
  52.  * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
  53.  * @since 1.5
  54.  */
  55. public class Base32OutputStream extends BaseNCodecOutputStream {

  56.     /**
  57.      * Constructs a Base32OutputStream such that all data written is Base32-encoded to the original provided OutputStream.
  58.      *
  59.      * @param outputStream
  60.      *            OutputStream to wrap.
  61.      */
  62.     public Base32OutputStream(final OutputStream outputStream) {
  63.         this(outputStream, true);
  64.     }

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

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

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

  118. }