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

  19. /**
  20.  * Provides Base32 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 Base32InputStream is to DECODE, whereas the default behaviour of the Base32OutputStream
  25.  * is to ENCODE, but this behaviour can be overridden by using a different constructor.
  26.  * </p>
  27.  * <p>
  28.  * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
  29.  * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
  30.  * </p>
  31.  *
  32.  * @version $Id: Base32InputStream.java 1586299 2014-04-10 13:50:21Z ggregory $
  33.  * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
  34.  * @since 1.5
  35.  */
  36. public class Base32InputStream extends BaseNCodecInputStream {

  37.     /**
  38.      * Creates a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream.
  39.      *
  40.      * @param in
  41.      *            InputStream to wrap.
  42.      */
  43.     public Base32InputStream(final InputStream in) {
  44.         this(in, false);
  45.     }

  46.     /**
  47.      * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
  48.      * provided InputStream.
  49.      *
  50.      * @param in
  51.      *            InputStream to wrap.
  52.      * @param doEncode
  53.      *            true if we should encode all data read from us, false if we should decode.
  54.      */
  55.     public Base32InputStream(final InputStream in, final boolean doEncode) {
  56.         super(in, new Base32(false), doEncode);
  57.     }

  58.     /**
  59.      * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
  60.      * provided InputStream.
  61.      *
  62.      * @param in
  63.      *            InputStream to wrap.
  64.      * @param doEncode
  65.      *            true if we should encode all data read from us, false if we should decode.
  66.      * @param lineLength
  67.      *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
  68.      *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
  69.      *            is false, lineLength is ignored.
  70.      * @param lineSeparator
  71.      *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
  72.      *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
  73.      */
  74.     public Base32InputStream(final InputStream in, final boolean doEncode,
  75.                              final int lineLength, final byte[] lineSeparator) {
  76.         super(in, new Base32(lineLength, lineSeparator), doEncode);
  77.     }

  78. }