|
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.FilterInputStream; |
21 | | -import java.io.IOException; |
22 | | -import java.io.InputStream; |
23 | | - |
24 | | -/** |
25 | | - * Provides Base64 encoding and decoding in a streaming fashion (unlimited size). |
26 | | - * When encoding the default lineLength is 76 characters and the default |
27 | | - * lineEnding is CRLF, but these can be overridden by using the appropriate |
28 | | - * constructor. |
29 | | - * <p> |
30 | | - * The default behaviour of the Base64InputStream is to DECODE, whereas the |
31 | | - * default behaviour of the Base64OutputStream is to ENCODE, but this |
32 | | - * behaviour can be overridden by using a different constructor. |
33 | | - * </p><p> |
34 | | - * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose |
35 | | - * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein. |
36 | | - * </p><p> |
37 | | - * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode |
38 | | - * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc). |
39 | | - * </p> |
40 | | - * @author Apache Software Foundation |
41 | | - * @version $Id $ |
42 | | - * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> |
43 | | - * @since 1.4 |
44 | | - */ |
45 | | -public class Base64InputStream extends FilterInputStream { |
46 | | - |
47 | | - private final boolean doEncode; |
48 | | - private final Base64 base64; |
49 | | - private final byte[] singleByte = new byte[1]; |
50 | | - |
51 | | - /** |
52 | | - * Creates a Base64InputStream such that all data read is Base64-decoded |
53 | | - * from the original provided InputStream. |
54 | | - * |
55 | | - * @param in InputStream to wrap. |
56 | | - */ |
57 | | - public Base64InputStream(InputStream in) { |
58 | | - this(in, false); |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * Creates a Base64InputStream such that all data read is either |
63 | | - * Base64-encoded or Base64-decoded from the original provided InputStream. |
64 | | - * |
65 | | - * @param in InputStream to wrap. |
66 | | - * @param doEncode true if we should encode all data read from us, |
67 | | - * false if we should decode. |
68 | | - */ |
69 | | - public Base64InputStream(InputStream in, boolean doEncode) { |
70 | | - super(in); |
71 | | - this.doEncode = doEncode; |
72 | | - this.base64 = new Base64(); |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * Creates a Base64InputStream such that all data read is either |
77 | | - * Base64-encoded or Base64-decoded from the original provided InputStream. |
78 | | - * |
79 | | - * @param in InputStream to wrap. |
80 | | - * @param doEncode true if we should encode all data read from us, |
81 | | - * false if we should decode. |
82 | | - * @param lineLength If doEncode is true, each line of encoded |
83 | | - * data will contain lineLength characters. |
84 | | - * If lineLength <=0, the encoded data is not divided into lines. |
85 | | - * If doEncode is false, lineLength is ignored. |
86 | | - * @param lineSeparator If doEncode is true, each line of encoded |
87 | | - * data will be terminated with this byte sequence (e.g. \r\n). |
88 | | - * If lineLength <= 0, the lineSeparator is not used. |
89 | | - * If doEncode is false lineSeparator is ignored. |
90 | | - */ |
91 | | - public Base64InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) { |
92 | | - super(in); |
93 | | - this.doEncode = doEncode; |
94 | | - this.base64 = new Base64(lineLength, lineSeparator); |
95 | | - } |
96 | | - |
97 | | - /** |
98 | | - * Reads one <code>byte</code> from this input stream. |
99 | | - * |
100 | | - * @return the byte as an integer in the range 0 to 255 |
101 | | - * Returns -1 if EOF has been reached. |
102 | | - */ |
103 | | - public int read() throws IOException { |
104 | | - int r = read(singleByte, 0, 1); |
105 | | - while (r == 0) { |
106 | | - r = read(singleByte, 0, 1); |
107 | | - } |
108 | | - if (r > 0) { |
109 | | - return singleByte[0] < 0 ? 256 + singleByte[0] : singleByte[0]; |
110 | | - } |
111 | | - return -1; |
112 | | - } |
113 | | - |
114 | | - /** |
115 | | - * Attempts to read <code>len</code> bytes into the specified |
116 | | - * <code>b</code> array starting at <code>offset</code> from |
117 | | - * this InputStream. |
118 | | - * |
119 | | - * @param b destination byte array |
120 | | - * @param offset where to start writing the bytes |
121 | | - * @param len maximum number of bytes to read |
122 | | - * |
123 | | - * @return number of bytes read |
124 | | - * @throws IOException if an I/O error occurs. |
125 | | - * @throws NullPointerException if the byte array parameter is null |
126 | | - * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid |
127 | | - */ |
128 | | - public int read(byte b[], int offset, int len) throws IOException { |
129 | | - if (b == null) { |
130 | | - throw new NullPointerException(); |
131 | | - } else if (offset < 0 || len < 0) { |
132 | | - throw new IndexOutOfBoundsException(); |
133 | | - } else if (offset > b.length || offset + len > b.length) { |
134 | | - throw new IndexOutOfBoundsException(); |
135 | | - } else if (len == 0) { |
136 | | - return 0; |
137 | | - } else { |
138 | | - if (!base64.hasData()) { |
139 | | - byte[] buf = new byte[doEncode ? 4096 : 8192]; |
140 | | - int c = in.read(buf); |
141 | | - |
142 | | - // A little optimization to avoid System.arraycopy() |
143 | | - // when possible. |
144 | | - if (c > 0 && b.length == len) { |
145 | | - base64.setInitialBuffer(b, offset, len); |
146 | | - } |
147 | | - |
148 | | - if (doEncode) { |
149 | | - base64.encode(buf, 0, c); |
150 | | - } else { |
151 | | - base64.decode(buf, 0, c); |
152 | | - } |
153 | | - } |
154 | | - return base64.readResults(b, offset, len); |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - /** |
159 | | - * {@inheritDoc} |
160 | | - * @return false |
161 | | - */ |
162 | | - public boolean markSupported() { |
163 | | - return false; // not an easy job to support marks |
164 | | - } |
165 | | -} |
| 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.FilterInputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | + |
| 24 | +/** |
| 25 | + * Provides Base64 encoding and decoding in a streaming fashion (unlimited size). |
| 26 | + * When encoding the default lineLength is 76 characters and the default |
| 27 | + * lineEnding is CRLF, but these can be overridden by using the appropriate |
| 28 | + * constructor. |
| 29 | + * <p> |
| 30 | + * The default behaviour of the Base64InputStream is to DECODE, whereas the |
| 31 | + * default behaviour of the Base64OutputStream is to ENCODE, but this |
| 32 | + * behaviour can be overridden by using a different constructor. |
| 33 | + * </p><p> |
| 34 | + * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose |
| 35 | + * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein. |
| 36 | + * </p><p> |
| 37 | + * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode |
| 38 | + * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc). |
| 39 | + * </p> |
| 40 | + * @author Apache Software Foundation |
| 41 | + * @version $Id $ |
| 42 | + * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> |
| 43 | + * @since 1.4 |
| 44 | + */ |
| 45 | +public class Base64InputStream extends FilterInputStream { |
| 46 | + |
| 47 | + private final boolean doEncode; |
| 48 | + private final Base64 base64; |
| 49 | + private final byte[] singleByte = new byte[1]; |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a Base64InputStream such that all data read is Base64-decoded |
| 53 | + * from the original provided InputStream. |
| 54 | + * |
| 55 | + * @param in InputStream to wrap. |
| 56 | + */ |
| 57 | + public Base64InputStream(InputStream in) { |
| 58 | + this(in, false); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a Base64InputStream such that all data read is either |
| 63 | + * Base64-encoded or Base64-decoded from the original provided InputStream. |
| 64 | + * |
| 65 | + * @param in InputStream to wrap. |
| 66 | + * @param doEncode true if we should encode all data read from us, |
| 67 | + * false if we should decode. |
| 68 | + */ |
| 69 | + public Base64InputStream(InputStream in, boolean doEncode) { |
| 70 | + super(in); |
| 71 | + this.doEncode = doEncode; |
| 72 | + this.base64 = new Base64(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a Base64InputStream such that all data read is either |
| 77 | + * Base64-encoded or Base64-decoded from the original provided InputStream. |
| 78 | + * |
| 79 | + * @param in InputStream to wrap. |
| 80 | + * @param doEncode true if we should encode all data read from us, |
| 81 | + * false if we should decode. |
| 82 | + * @param lineLength If doEncode is true, each line of encoded |
| 83 | + * data will contain lineLength characters. |
| 84 | + * If lineLength <=0, the encoded data is not divided into lines. |
| 85 | + * If doEncode is false, lineLength is ignored. |
| 86 | + * @param lineSeparator If doEncode is true, each line of encoded |
| 87 | + * data will be terminated with this byte sequence (e.g. \r\n). |
| 88 | + * If lineLength <= 0, the lineSeparator is not used. |
| 89 | + * If doEncode is false lineSeparator is ignored. |
| 90 | + */ |
| 91 | + public Base64InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) { |
| 92 | + super(in); |
| 93 | + this.doEncode = doEncode; |
| 94 | + this.base64 = new Base64(lineLength, lineSeparator); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Reads one <code>byte</code> from this input stream. |
| 99 | + * |
| 100 | + * @return the byte as an integer in the range 0 to 255 |
| 101 | + * Returns -1 if EOF has been reached. |
| 102 | + */ |
| 103 | + public int read() throws IOException { |
| 104 | + int r = read(singleByte, 0, 1); |
| 105 | + while (r == 0) { |
| 106 | + r = read(singleByte, 0, 1); |
| 107 | + } |
| 108 | + if (r > 0) { |
| 109 | + return singleByte[0] < 0 ? 256 + singleByte[0] : singleByte[0]; |
| 110 | + } |
| 111 | + return -1; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Attempts to read <code>len</code> bytes into the specified |
| 116 | + * <code>b</code> array starting at <code>offset</code> from |
| 117 | + * this InputStream. |
| 118 | + * |
| 119 | + * @param b destination byte array |
| 120 | + * @param offset where to start writing the bytes |
| 121 | + * @param len maximum number of bytes to read |
| 122 | + * |
| 123 | + * @return number of bytes read |
| 124 | + * @throws IOException if an I/O error occurs. |
| 125 | + * @throws NullPointerException if the byte array parameter is null |
| 126 | + * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid |
| 127 | + */ |
| 128 | + public int read(byte b[], int offset, int len) throws IOException { |
| 129 | + if (b == null) { |
| 130 | + throw new NullPointerException(); |
| 131 | + } else if (offset < 0 || len < 0) { |
| 132 | + throw new IndexOutOfBoundsException(); |
| 133 | + } else if (offset > b.length || offset + len > b.length) { |
| 134 | + throw new IndexOutOfBoundsException(); |
| 135 | + } else if (len == 0) { |
| 136 | + return 0; |
| 137 | + } else { |
| 138 | + if (!base64.hasData()) { |
| 139 | + byte[] buf = new byte[doEncode ? 4096 : 8192]; |
| 140 | + int c = in.read(buf); |
| 141 | + |
| 142 | + // A little optimization to avoid System.arraycopy() |
| 143 | + // when possible. |
| 144 | + if (c > 0 && b.length == len) { |
| 145 | + base64.setInitialBuffer(b, offset, len); |
| 146 | + } |
| 147 | + |
| 148 | + if (doEncode) { |
| 149 | + base64.encode(buf, 0, c); |
| 150 | + } else { |
| 151 | + base64.decode(buf, 0, c); |
| 152 | + } |
| 153 | + } |
| 154 | + return base64.readResults(b, offset, len); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * {@inheritDoc} |
| 160 | + * @return false |
| 161 | + */ |
| 162 | + public boolean markSupported() { |
| 163 | + return false; // not an easy job to support marks |
| 164 | + } |
| 165 | +} |
0 commit comments