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.OutputStream;
21  
22  /**
23   * Provides Base32 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
24   * is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
25   * constructor.
26   * <p>
27   * The default behaviour of the Base32OutputStream is to ENCODE, whereas the default behaviour of the Base32InputStream
28   * is to DECODE. But this behaviour can be overridden by using a different constructor.
29   * </p>
30   * <p>
31   * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
32   * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
33   * </p>
34   * <p>
35   * <b>Note:</b> It is mandatory to close the stream after the last byte has been written to it, otherwise the
36   * final padding will be omitted and the resulting data will be incomplete/inconsistent.
37   * </p>
38   *
39   * @version $Id: Base32OutputStream.java 1635952 2014-11-01 14:19:04Z tn $
40   * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
41   * @since 1.5
42   */
43  public class Base32OutputStream extends BaseNCodecOutputStream {
44  
45      /**
46       * Creates a Base32OutputStream such that all data written is Base32-encoded to the original provided OutputStream.
47       *
48       * @param out
49       *            OutputStream to wrap.
50       */
51      public Base32OutputStream(final OutputStream out) {
52          this(out, true);
53      }
54  
55      /**
56       * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the
57       * original provided OutputStream.
58       *
59       * @param out
60       *            OutputStream to wrap.
61       * @param doEncode
62       *            true if we should encode all data written to us, false if we should decode.
63       */
64      public Base32OutputStream(final OutputStream out, final boolean doEncode) {
65          super(out, new Base32(false), doEncode);
66      }
67  
68      /**
69       * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the
70       * original provided OutputStream.
71       *
72       * @param out
73       *            OutputStream to wrap.
74       * @param doEncode
75       *            true if we should encode all data written to us, false if we should decode.
76       * @param lineLength
77       *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
78       *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
79       *            is false, lineLength is ignored.
80       * @param lineSeparator
81       *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
82       *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
83       */
84      public Base32OutputStream(final OutputStream out, final boolean doEncode,
85                                final int lineLength, final byte[] lineSeparator) {
86          super(out, new Base32(lineLength, lineSeparator), doEncode);
87      }
88  
89  }