Skip to content

Commit c6fadee

Browse files
committed
New classes for Base32 rework
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1063760 13f79535-47bb-0310-9956-ffa450edef68
1 parent ee4b722 commit c6fadee

5 files changed

Lines changed: 715 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
/**
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 Base32InputStream is to DECODE, whereas the default behaviour of the Base32OutputStream
28+
* is to ENCODE, 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+
*
35+
* @version $Revision$
36+
* @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
37+
* @since 1.5
38+
*/
39+
public class Base32InputStream extends BasedCodecInputStream {
40+
41+
/**
42+
* Creates a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream.
43+
*
44+
* @param in
45+
* InputStream to wrap.
46+
*/
47+
public Base32InputStream(InputStream in) {
48+
this(in, false);
49+
}
50+
51+
/**
52+
* Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
53+
* provided InputStream.
54+
*
55+
* @param in
56+
* InputStream to wrap.
57+
* @param doEncode
58+
* true if we should encode all data read from us, false if we should decode.
59+
*/
60+
public Base32InputStream(InputStream in, boolean doEncode) {
61+
super(in, new Base32(false), doEncode);
62+
}
63+
64+
/**
65+
* Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
66+
* provided InputStream.
67+
*
68+
* @param in
69+
* InputStream to wrap.
70+
* @param doEncode
71+
* true if we should encode all data read from us, false if we should decode.
72+
* @param lineLength
73+
* If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
74+
* nearest multiple of 4). If lineLength <=0, the encoded data is not divided into lines. If doEncode is
75+
* false, lineLength is ignored.
76+
* @param lineSeparator
77+
* If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
78+
* If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
79+
*/
80+
public Base32InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) {
81+
super(in, new Base32(lineLength, lineSeparator), doEncode);
82+
}
83+
84+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 Base64 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 Base64OutputStream is to ENCODE, whereas the default behaviour of the Base64InputStream
28+
* is to DECODE. But this behaviour can be overridden by using a different constructor.
29+
* </p>
30+
* <p>
31+
* This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
32+
* Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
33+
* </p>
34+
* <p>
35+
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
36+
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
37+
* </p>
38+
*
39+
* @author Apache Software Foundation
40+
* @version $Id$
41+
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
42+
* @since 1.4
43+
*/
44+
public class Base32OutputStream extends BasedCodecOutputStream {
45+
46+
/**
47+
* Creates a Base64OutputStream such that all data written is Base64-encoded to the original provided OutputStream.
48+
*
49+
* @param out
50+
* OutputStream to wrap.
51+
*/
52+
public Base32OutputStream(OutputStream out) {
53+
this(out, true);
54+
}
55+
56+
/**
57+
* Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
58+
* original provided OutputStream.
59+
*
60+
* @param out
61+
* OutputStream to wrap.
62+
* @param doEncode
63+
* true if we should encode all data written to us, false if we should decode.
64+
*/
65+
public Base32OutputStream(OutputStream out, boolean doEncode) {
66+
super(out, new Base32(false), doEncode);
67+
}
68+
69+
/**
70+
* Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
71+
* original provided OutputStream.
72+
*
73+
* @param out
74+
* OutputStream to wrap.
75+
* @param doEncode
76+
* true if we should encode all data written to us, false if we should decode.
77+
* @param lineLength
78+
* If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
79+
* nearest multiple of 4). If lineLength <=0, the encoded data is not divided into lines. If doEncode is
80+
* false, lineLength is ignored.
81+
* @param lineSeparator
82+
* If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
83+
* If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
84+
*/
85+
public Base32OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) {
86+
super(out, new Base32(lineLength, lineSeparator), doEncode);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)