Base32InputStream.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.InputStream;

  19. import org.apache.commons.codec.CodecPolicy;

  20. /**
  21.  * Provides Base32 decoding 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 Base32InputStream is to DECODE, whereas the default behavior of the Base32OutputStream
  26.  * is to ENCODE, 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.  * You can set the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a
  34.  * valid encoding. These can be bits that are unused from the final character or entire characters. The default mode is
  35.  * lenient decoding.
  36.  * </p>
  37.  * <ul>
  38.  * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.
  39.  * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid
  40.  * encoding. Any unused bits from the final character must be zero. Impossible counts of entire final characters are not
  41.  * allowed.
  42.  * </ul>
  43.  * <p>
  44.  * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches
  45.  * the original, i.e. no changes occur on the final character. This requires that the input bytes use the same padding
  46.  * and alphabet as the encoder.
  47.  * </p>
  48.  * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
  49.  * @since 1.5
  50.  */
  51. public class Base32InputStream extends BaseNCodecInputStream {

  52.     /**
  53.      * Constructs a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream.
  54.      *
  55.      * @param inputStream
  56.      *            InputStream to wrap.
  57.      */
  58.     public Base32InputStream(final InputStream inputStream) {
  59.         this(inputStream, false);
  60.     }

  61.     /**
  62.      * Constructs a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
  63.      * provided InputStream.
  64.      *
  65.      * @param inputStream
  66.      *            InputStream to wrap.
  67.      * @param doEncode
  68.      *            true if we should encode all data read from us, false if we should decode.
  69.      */
  70.     public Base32InputStream(final InputStream inputStream, final boolean doEncode) {
  71.         super(inputStream, new Base32(false), doEncode);
  72.     }

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

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

  115. }