49
49
* "Base64DecodeStream: Bad Padding byte (1)."
52
* @version $Id: Base64DecodeStream.java 1345683 2012-06-03 14:50:33Z gadams $
52
* @version $Id: Base64DecodeStream.java 1610846 2014-07-15 20:44:18Z vhennebert $
54
54
* Originally authored by Thomas DeWeese, Vincent Hardy, and Chuck McManis.
65
private static final byte[] pem_array = new byte[256];
65
private static final byte[] PEM_ARRAY = new byte[256];
67
for (int i=0; i<pem_array.length; i++)
67
for (int i = 0; i < PEM_ARRAY.length; i++) {
71
for (char c='A'; c<='Z'; c++) {
72
pem_array[c] = (byte)idx++;
74
for (char c='a'; c<='z'; c++) {
75
pem_array[c] = (byte)idx++;
78
for (char c='0'; c<='9'; c++) {
79
pem_array[c] = (byte)idx++;
82
pem_array['+'] = (byte)idx++;
83
pem_array['/'] = (byte)idx++;
72
for (char c = 'A'; c <= 'Z'; c++) {
73
PEM_ARRAY[c] = (byte)idx++;
75
for (char c = 'a'; c <= 'z'; c++) {
76
PEM_ARRAY[c] = (byte)idx++;
79
for (char c = '0'; c <= '9'; c++) {
80
PEM_ARRAY[c] = (byte)idx++;
83
PEM_ARRAY['+'] = (byte)idx++;
84
PEM_ARRAY['/'] = (byte)idx++;
86
87
public boolean markSupported() { return false; }
88
89
public void close()
89
90
throws IOException {
93
94
public int available()
94
95
throws IOException {
98
byte[] decode_buffer = new byte[4];
99
byte[] out_buffer = new byte[3];
99
byte[] decodeBuffer = new byte[4];
100
byte[] outBuffer = new byte[3];
103
104
public int read() throws IOException {
105
if (out_offset == 3) {
106
if (EOF || getNextAtom()) {
106
if (outOffset == 3) {
107
if (eof || getNextAtom()) {
112
return ((int)out_buffer[out_offset++])&0xFF;
113
return ((int)outBuffer[outOffset++]) & 0xFF;
115
116
public int read(byte []out, int offset, int len)
135
139
final boolean getNextAtom() throws IOException {
136
int count, a, b, c, d;
140
count = src.read(decode_buffer, off, 4-off);
148
count = src.read(decodeBuffer, off, 4 - off);
145
while(in < off+count) {
146
if ((decode_buffer[in] != '\n') &&
147
(decode_buffer[in] != '\r') &&
148
(decode_buffer[in] != ' '))
149
decode_buffer[out++] = decode_buffer[in];
155
while (in < off + count) {
156
if ((decodeBuffer[in] != '\n')
157
&& (decodeBuffer[in] != '\r')
158
&& (decodeBuffer[in] != ' ')) {
159
decodeBuffer[out++] = decodeBuffer[in];
156
a = pem_array[((int)decode_buffer[0])&0xFF];
157
b = pem_array[((int)decode_buffer[1])&0xFF];
158
c = pem_array[((int)decode_buffer[2])&0xFF];
159
d = pem_array[((int)decode_buffer[3])&0xFF];
161
out_buffer[0] = (byte)((a<<2) | (b>>>4));
162
out_buffer[1] = (byte)((b<<4) | (c>>>2));
163
out_buffer[2] = (byte)((c<<6) | d );
165
if (decode_buffer[3] != '=') {
167
a = PEM_ARRAY[((int)decodeBuffer[0]) & 0xFF];
168
b = PEM_ARRAY[((int)decodeBuffer[1]) & 0xFF];
169
c = PEM_ARRAY[((int)decodeBuffer[2]) & 0xFF];
170
d = PEM_ARRAY[((int)decodeBuffer[3]) & 0xFF];
172
outBuffer[0] = (byte)((a << 2) | (b >>> 4));
173
outBuffer[1] = (byte)((b << 4) | (c >>> 2));
174
outBuffer[2] = (byte)((c << 6) | d);
176
if (decodeBuffer[3] != '=') {
166
177
// All three bytes are good.
168
} else if (decode_buffer[2] == '=') {
179
} else if (decodeBuffer[2] == '=') {
169
180
// Only one byte of output.
170
out_buffer[2] = out_buffer[0];
181
outBuffer[2] = outBuffer[0];
174
185
// Only two bytes of output.
175
out_buffer[2] = out_buffer[1];
176
out_buffer[1] = out_buffer[0];
186
outBuffer[2] = outBuffer[1];
187
outBuffer[1] = outBuffer[0];