forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Variant.java
More file actions
362 lines (320 loc) · 11.8 KB
/
Base64Variant.java
File metadata and controls
362 lines (320 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Jackson JSON-processor.
*
* Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
*
* Licensed under the License specified in file LICENSE, included with
* the source code and binary code bundles.
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.jackson;
import java.util.Arrays;
/**
* Abstract base class used to define specific details of which
* variant of Base64 encoding/decoding is to be used. Although there is
* somewhat standard basic version (so-called "MIME Base64"), other variants
* exists, see <a href="http://en.wikipedia.org/wiki/Base64">Base64 Wikipedia entry</a> for details.
*
* @author Tatu Saloranta
*
* @since 0.9.3
*/
public final class Base64Variant
{
/**
* Placeholder used by "no padding" variant, to be used when a character
* value is needed.
*/
final static char PADDING_CHAR_NONE = '\0';
/**
* Marker used to denote ascii characters that do not correspond
* to a 6-bit value (in this variant), and is not used as a padding
* character.
*/
public final static int BASE64_VALUE_INVALID = -1;
/**
* Marker used to denote ascii character (in decoding table) that
* is the padding character using this variant (if any).
*/
public final static int BASE64_VALUE_PADDING = -2;
/*
////////////////////////////////////////////////////
// Encoding/decoding tables
////////////////////////////////////////////////////
*/
/**
* Decoding table used for base 64 decoding.
*/
private final int[] _asciiToBase64 = new int[128];
/**
* Encoding table used for base 64 decoding when output is done
* as characters.
*/
private final char[] _base64ToAsciiC = new char[64];
/**
* Alternative encoding table used for base 64 decoding when output is done
* as ascii bytes.
*/
private final byte[] _base64ToAsciiB = new byte[64];
/*
////////////////////////////////////////////////////
// Other configuration
////////////////////////////////////////////////////
*/
/**
* Symbolic name of variant; used for diagnostics/debugging.
*/
final String _name;
/**
* Whether this variant uses padding or not.
*/
final boolean _usesPadding;
/**
* Characted used for padding, if any ({@link #PADDING_CHAR_NONE} if not).
*/
final char _paddingChar;
/**
* Maximum number of encoded base64 characters to output during encoding
* before adding a linefeed, if line length is to be limited
* ({@link java.lang.Integer#MAX_VALUE} if not limited).
*<p>
* Note: for some output modes (when writing attributes) linefeeds may
* need to be avoided, and this value ignored.
*/
final int _maxLineLength;
/*
////////////////////////////////////////////////////
// Life-cycle
////////////////////////////////////////////////////
*/
public Base64Variant(String name, String base64Alphabet, boolean usesPadding, char paddingChar, int maxLineLength)
{
_name = name;
_usesPadding = usesPadding;
_paddingChar = paddingChar;
_maxLineLength = maxLineLength;
// Ok and then we need to create codec tables.
// First the main encoding table:
int alphaLen = base64Alphabet.length();
if (alphaLen != 64) {
throw new IllegalArgumentException("Base64Alphabet length must be exactly 64 (was "+alphaLen+")");
}
// And then secondary encoding table and decoding table:
base64Alphabet.getChars(0, alphaLen, _base64ToAsciiC, 0);
Arrays.fill(_asciiToBase64, BASE64_VALUE_INVALID);
for (int i = 0; i < alphaLen; ++i) {
char alpha = _base64ToAsciiC[i];
_base64ToAsciiB[i] = (byte) alpha;
_asciiToBase64[alpha] = i;
}
// Plus if we use padding, add that in too
if (usesPadding) {
_asciiToBase64[(int) paddingChar] = BASE64_VALUE_PADDING;
}
}
/**
* "Copy constructor" that can be used when the base alphabet is identical
* to one used by another variant except for the maximum line length
* (and obviously, name).
* @param base Undocumented.
* @param name Undocumented.
* @param maxLineLength Undocumented.
*/
public Base64Variant(Base64Variant base, String name, int maxLineLength)
{
this(base, name, base._usesPadding, base._paddingChar, maxLineLength);
}
/**
* "Copy constructor" that can be used when the base alphabet is identical
* to one used by another variant, but other details (padding, maximum
* line length) differ
* @param base Undocumented.
* @param name Undocumented.
* @param usesPadding Undocumented.
* @param paddingChar Undocumented.
* @param maxLineLength Undocumented.
*/
public Base64Variant(Base64Variant base, String name, boolean usesPadding, char paddingChar, int maxLineLength)
{
_name = name;
byte[] srcB = base._base64ToAsciiB;
System.arraycopy(srcB, 0, this._base64ToAsciiB, 0, srcB.length);
char[] srcC = base._base64ToAsciiC;
System.arraycopy(srcC, 0, this._base64ToAsciiC, 0, srcC.length);
int[] srcV = base._asciiToBase64;
System.arraycopy(srcV, 0, this._asciiToBase64, 0, srcV.length);
_usesPadding = usesPadding;
_paddingChar = paddingChar;
_maxLineLength = maxLineLength;
}
/*
////////////////////////////////////////////////////
// Public accessors
////////////////////////////////////////////////////
*/
public String getName() { return _name; }
public boolean usesPadding() { return _usesPadding; }
public boolean usesPaddingChar(char c) { return c == _paddingChar; }
public boolean usesPaddingChar(int ch) { return ch == (int) _paddingChar; }
public char getPaddingChar() { return _paddingChar; }
public byte getPaddingByte() { return (byte)_paddingChar; }
public int getMaxLineLength() { return _maxLineLength; }
/*
////////////////////////////////////////////////////
// Decoding support
////////////////////////////////////////////////////
*/
/**
* @param c Undocumented.
* @return 6-bit decoded value, if valid character;
*/
public int decodeBase64Char(char c)
{
int ch = (int) c;
return (ch <= 127) ? _asciiToBase64[ch] : BASE64_VALUE_INVALID;
}
public int decodeBase64Char(int ch)
{
return (ch <= 127) ? _asciiToBase64[ch] : BASE64_VALUE_INVALID;
}
public int decodeBase64Byte(byte b)
{
int ch = (int) b;
return (ch <= 127) ? _asciiToBase64[ch] : BASE64_VALUE_INVALID;
}
/*
////////////////////////////////////////////////////
// Encoding support
////////////////////////////////////////////////////
*/
public char encodeBase64BitsAsChar(int value)
{
/* Let's assume caller has done necessary checks; this
* method must be fast and inlinable
*/
return _base64ToAsciiC[value];
}
/**
* Method that encodes given right-aligned (LSB) 24-bit value
* into 4 base64 characters, stored in given result buffer.
* @param b24 Undocumented.
* @param buffer Undocumented.
* @param ptr Undocumented.
* @return Undocumented.
*/
public int encodeBase64Chunk(int b24, char[] buffer, int ptr)
{
buffer[ptr++] = _base64ToAsciiC[(b24 >> 18) & 0x3F];
buffer[ptr++] = _base64ToAsciiC[(b24 >> 12) & 0x3F];
buffer[ptr++] = _base64ToAsciiC[(b24 >> 6) & 0x3F];
buffer[ptr++] = _base64ToAsciiC[b24 & 0x3F];
return ptr;
}
public void encodeBase64Chunk(StringBuilder sb, int b24)
{
sb.append(_base64ToAsciiC[(b24 >> 18) & 0x3F]);
sb.append(_base64ToAsciiC[(b24 >> 12) & 0x3F]);
sb.append(_base64ToAsciiC[(b24 >> 6) & 0x3F]);
sb.append(_base64ToAsciiC[b24 & 0x3F]);
}
/**
* Method that outputs partial chunk (which only encodes one
* or two bytes of data).Data given is still aligned same as if
it as full data; that is, missing data is at the "right end"
(LSB) of int.
*
* @param bits Undocumented.
* @param outputBytes Number of encoded bytes included (either 1 or 2)
* @param buffer Undocumented.
* @param outPtr Undocumented.
* @return Undocumented.
*/
public int encodeBase64Partial(int bits, int outputBytes, char[] buffer, int outPtr)
{
buffer[outPtr++] = _base64ToAsciiC[(bits >> 18) & 0x3F];
buffer[outPtr++] = _base64ToAsciiC[(bits >> 12) & 0x3F];
if (_usesPadding) {
buffer[outPtr++] = (outputBytes == 2) ?
_base64ToAsciiC[(bits >> 6) & 0x3F] : _paddingChar;
buffer[outPtr++] = _paddingChar;
} else {
if (outputBytes == 2) {
buffer[outPtr++] = _base64ToAsciiC[(bits >> 6) & 0x3F];
}
}
return outPtr;
}
public void encodeBase64Partial(StringBuilder sb, int bits, int outputBytes)
{
sb.append(_base64ToAsciiC[(bits >> 18) & 0x3F]);
sb.append(_base64ToAsciiC[(bits >> 12) & 0x3F]);
if (_usesPadding) {
sb.append((outputBytes == 2) ?
_base64ToAsciiC[(bits >> 6) & 0x3F] : _paddingChar);
sb.append(_paddingChar);
} else {
if (outputBytes == 2) {
sb.append(_base64ToAsciiC[(bits >> 6) & 0x3F]);
}
}
}
public byte encodeBase64BitsAsByte(int value)
{
// As with above, assuming it is 6-bit value
return _base64ToAsciiB[value];
}
/**
* Method that encodes given right-aligned (LSB) 24-bit value
* into 4 base64 bytes (ascii), stored in given result buffer.
* @param b24 Undocumented.
* @param buffer Undocumented.
* @param ptr Undocumented.
* @return Undocumented.
*/
public int encodeBase64Chunk(int b24, byte[] buffer, int ptr)
{
buffer[ptr++] = _base64ToAsciiB[(b24 >> 18) & 0x3F];
buffer[ptr++] = _base64ToAsciiB[(b24 >> 12) & 0x3F];
buffer[ptr++] = _base64ToAsciiB[(b24 >> 6) & 0x3F];
buffer[ptr++] = _base64ToAsciiB[b24 & 0x3F];
return ptr;
}
/**
* Method that outputs partial chunk (which only encodes one or two bytes of data).Data given is still aligned same
* as if it as full data; that is, missing data is at the "right end" (LSB) of int.
*
* @param bits Undocumented.
* @param outputBytes Number of encoded bytes included (either 1 or 2)
* @param buffer Undocumented.
* @param outPtr Undocumented.
* @return Undocumented.
*/
public int encodeBase64Partial(int bits, int outputBytes, byte[] buffer, int outPtr)
{
buffer[outPtr++] = _base64ToAsciiB[(bits >> 18) & 0x3F];
buffer[outPtr++] = _base64ToAsciiB[(bits >> 12) & 0x3F];
if (_usesPadding) {
byte pb = (byte) _paddingChar;
buffer[outPtr++] = (outputBytes == 2) ?
_base64ToAsciiB[(bits >> 6) & 0x3F] : pb;
buffer[outPtr++] = pb;
} else {
if (outputBytes == 2) {
buffer[outPtr++] = _base64ToAsciiB[(bits >> 6) & 0x3F];
}
}
return outPtr;
}
/*
////////////////////////////////////////////////////
// other methods
////////////////////////////////////////////////////
*/
// @Override
public String toString() { return _name; }
}