View Javadoc
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  
18  package org.apache.commons.codec.binary;
19  
20  import java.io.InputStream;
21  
22  import org.apache.commons.codec.CodecPolicy;
23  
24  /**
25   * Provides Base32 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
26   * is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
27   * constructor.
28   * <p>
29   * The default behavior of the Base32InputStream is to DECODE, whereas the default behavior of the Base32OutputStream
30   * is to ENCODE, but this behavior can be overridden by using a different constructor.
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   * 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 Base32InputStream extends BaseNCodecInputStream {
56  
57      /**
58       * Creates a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream.
59       *
60       * @param inputStream
61       *            InputStream to wrap.
62       */
63      public Base32InputStream(final InputStream inputStream) {
64          this(inputStream, false);
65      }
66  
67      /**
68       * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
69       * provided InputStream.
70       *
71       * @param inputStream
72       *            InputStream to wrap.
73       * @param doEncode
74       *            true if we should encode all data read from us, false if we should decode.
75       */
76      public Base32InputStream(final InputStream inputStream, final boolean doEncode) {
77          super(inputStream, new Base32(false), doEncode);
78      }
79  
80      /**
81       * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
82       * provided InputStream.
83       *
84       * @param inputStream
85       *            InputStream to wrap.
86       * @param doEncode
87       *            true if we should encode all data read from us, false if we should decode.
88       * @param lineLength
89       *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
90       *            the nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If
91       *            doEncode is false, lineLength is ignored.
92       * @param lineSeparator
93       *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
94       *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
95       */
96      public Base32InputStream(final InputStream inputStream, final boolean doEncode, final int lineLength, final byte[] lineSeparator) {
97          super(inputStream, new Base32(lineLength, lineSeparator), doEncode);
98      }
99  
100     /**
101      * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
102      * provided InputStream.
103      *
104      * @param inputStream
105      *            InputStream to wrap.
106      * @param doEncode
107      *            true if we should encode all data read from 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 (e.g. \r\n).
114      *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
115      * @param decodingPolicy
116      *            The decoding policy.
117      * @since 1.15
118      */
119     public Base32InputStream(final InputStream inputStream, final boolean doEncode, final int lineLength, final byte[] lineSeparator,
120         final CodecPolicy decodingPolicy) {
121         super(inputStream, new Base32(lineLength, lineSeparator, false, BaseNCodec.PAD_DEFAULT, decodingPolicy), doEncode);
122     }
123 
124 }