| 1 |
package com.akdeniz.googleplaycrawler.misc; |
| 2 |
/* |
| 3 |
* Copyright (C) 2010 The Android Open Source Project |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* 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 |
import java.io.UnsupportedEncodingException; |
| 19 |
|
| 20 |
/** |
| 21 |
* Utilities for encoding and decoding the Base64 representation of |
| 22 |
* binary data. See RFCs <a |
| 23 |
* href="http://www.ietf.org/rfc/rfc2045.txt">2045</a> and <a |
| 24 |
* href="http://www.ietf.org/rfc/rfc3548.txt">3548</a>. |
| 25 |
*/ |
| 26 |
public class Base64 { |
| 27 |
/** |
| 28 |
* Default values for encoder/decoder flags. |
| 29 |
*/ |
| 30 |
public static final int DEFAULT = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Encoder flag bit to omit the padding '=' characters at the end |
| 34 |
* of the output (if any). |
| 35 |
*/ |
| 36 |
public static final int NO_PADDING = 1; |
| 37 |
|
| 38 |
/** |
| 39 |
* Encoder flag bit to omit all line terminators (i.e., the output |
| 40 |
* will be on one long line). |
| 41 |
*/ |
| 42 |
public static final int NO_WRAP = 2; |
| 43 |
|
| 44 |
/** |
| 45 |
* Encoder flag bit to indicate lines should be terminated with a |
| 46 |
* CRLF pair instead of just an LF. Has no effect if {@code |
| 47 |
* NO_WRAP} is specified as well. |
| 48 |
*/ |
| 49 |
public static final int CRLF = 4; |
| 50 |
|
| 51 |
/** |
| 52 |
* Encoder/decoder flag bit to indicate using the "URL and |
| 53 |
* filename safe" variant of Base64 (see RFC 3548 section 4) where |
| 54 |
* {@code -} and {@code _} are used in place of {@code +} and |
| 55 |
* {@code /}. |
| 56 |
*/ |
| 57 |
public static final int URL_SAFE = 8; |
| 58 |
|
| 59 |
/** |
| 60 |
* Flag to pass to {@link Base64OutputStream} to indicate that it |
| 61 |
* should not close the output stream it is wrapping when it |
| 62 |
* itself is closed. |
| 63 |
*/ |
| 64 |
public static final int NO_CLOSE = 16; |
| 65 |
|
| 66 |
// -------------------------------------------------------- |
| 67 |
// shared code |
| 68 |
// -------------------------------------------------------- |
| 69 |
|
| 70 |
/* package */ static abstract class Coder { |
| 71 |
public byte[] output; |
| 72 |
public int op; |
| 73 |
|
| 74 |
/** |
| 75 |
* Encode/decode another block of input data. this.output is |
| 76 |
* provided by the caller, and must be big enough to hold all |
| 77 |
* the coded data. On exit, this.opwill be set to the length |
| 78 |
* of the coded data. |
| 79 |
* |
| 80 |
* @param finish true if this is the final call to process for |
| 81 |
* this object. Will finalize the coder state and |
| 82 |
* include any final bytes in the output. |
| 83 |
* |
| 84 |
* @return true if the input so far is good; false if some |
| 85 |
* error has been detected in the input stream.. |
| 86 |
*/ |
| 87 |
public abstract boolean process(byte[] input, int offset, int len, boolean finish); |
| 88 |
|
| 89 |
/** |
| 90 |
* @return the maximum number of bytes a call to process() |
| 91 |
* could produce for the given number of input bytes. This may |
| 92 |
* be an overestimate. |
| 93 |
*/ |
| 94 |
public abstract int maxOutputSize(int len); |
| 95 |
} |
| 96 |
|
| 97 |
// -------------------------------------------------------- |
| 98 |
// decoding |
| 99 |
// -------------------------------------------------------- |
| 100 |
|
| 101 |
/** |
| 102 |
* Decode the Base64-encoded data in input and return the data in |
| 103 |
* a new byte array. |
| 104 |
* |
| 105 |
* <p>The padding '=' characters at the end are considered optional, but |
| 106 |
* if any are present, there must be the correct number of them. |
| 107 |
* |
| 108 |
* @param str the input String to decode, which is converted to |
| 109 |
* bytes using the default charset |
| 110 |
* @param flags controls certain features of the decoded output. |
| 111 |
* Pass {@code DEFAULT} to decode standard Base64. |
| 112 |
* |
| 113 |
* @throws IllegalArgumentException if the input contains |
| 114 |
* incorrect padding |
| 115 |
*/ |
| 116 |
public static byte[] decode(String str, int flags) { |
| 117 |
return decode(str.getBytes(), flags); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Decode the Base64-encoded data in input and return the data in |
| 122 |
* a new byte array. |
| 123 |
* |
| 124 |
* <p>The padding '=' characters at the end are considered optional, but |
| 125 |
* if any are present, there must be the correct number of them. |
| 126 |
* |
| 127 |
* @param input the input array to decode |
| 128 |
* @param flags controls certain features of the decoded output. |
| 129 |
* Pass {@code DEFAULT} to decode standard Base64. |
| 130 |
* |
| 131 |
* @throws IllegalArgumentException if the input contains |
| 132 |
* incorrect padding |
| 133 |
*/ |
| 134 |
public static byte[] decode(byte[] input, int flags) { |
| 135 |
return decode(input, 0, input.length, flags); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Decode the Base64-encoded data in input and return the data in |
| 140 |
* a new byte array. |
| 141 |
* |
| 142 |
* <p>The padding '=' characters at the end are considered optional, but |
| 143 |
* if any are present, there must be the correct number of them. |
| 144 |
* |
| 145 |
* @param input the data to decode |
| 146 |
* @param offset the position within the input array at which to start |
| 147 |
* @param len the number of bytes of input to decode |
| 148 |
* @param flags controls certain features of the decoded output. |
| 149 |
* Pass {@code DEFAULT} to decode standard Base64. |
| 150 |
* |
| 151 |
* @throws IllegalArgumentException if the input contains |
| 152 |
* incorrect padding |
| 153 |
*/ |
| 154 |
public static byte[] decode(byte[] input, int offset, int len, int flags) { |
| 155 |
// Allocate space for the most data the input could represent. |
| 156 |
// (It could contain less if it contains whitespace, etc.) |
| 157 |
Decoder decoder = new Decoder(flags, new byte[len*3/4]); |
| 158 |
|
| 159 |
if (!decoder.process(input, offset, len, true)) { |
| 160 |
throw new IllegalArgumentException("bad base-64"); |
| 161 |
} |
| 162 |
|
| 163 |
// Maybe we got lucky and allocated exactly enough output space. |
| 164 |
if (decoder.op == decoder.output.length) { |
| 165 |
return decoder.output; |
| 166 |
} |
| 167 |
|
| 168 |
// Need to shorten the array, so allocate a new one of the |
| 169 |
// right size and copy. |
| 170 |
byte[] temp = new byte[decoder.op]; |
| 171 |
System.arraycopy(decoder.output, 0, temp, 0, decoder.op); |
| 172 |
return temp; |
| 173 |
} |
| 174 |
|
| 175 |
/* package */ static class Decoder extends Coder { |
| 176 |
/** |
| 177 |
* Lookup table for turning bytes into their position in the |
| 178 |
* Base64 alphabet. |
| 179 |
*/ |
| 180 |
private static final int DECODE[] = { |
| 181 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 182 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 183 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
| 184 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, |
| 185 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 186 |
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
| 187 |
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 188 |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 189 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 190 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 191 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 192 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 193 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 194 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 195 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 196 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 197 |
}; |
| 198 |
|
| 199 |
/** |
| 200 |
* Decode lookup table for the "web safe" variant (RFC 3548 |
| 201 |
* sec. 4) where - and _ replace + and /. |
| 202 |
*/ |
| 203 |
private static final int DECODE_WEBSAFE[] = { |
| 204 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 205 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 206 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, |
| 207 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, |
| 208 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 209 |
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, |
| 210 |
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 211 |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 212 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 213 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 214 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 215 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 216 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 217 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 218 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 219 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 220 |
}; |
| 221 |
|
| 222 |
/** Non-data values in the DECODE arrays. */ |
| 223 |
private static final int SKIP = -1; |
| 224 |
private static final int EQUALS = -2; |
| 225 |
|
| 226 |
/** |
| 227 |
* States 0-3 are reading through the next input tuple. |
| 228 |
* State 4 is having read one '=' and expecting exactly |
| 229 |
* one more. |
| 230 |
* State 5 is expecting no more data or padding characters |
| 231 |
* in the input. |
| 232 |
* State 6 is the error state; an error has been detected |
| 233 |
* in the input and no future input can "fix" it. |
| 234 |
*/ |
| 235 |
private int state; // state number (0 to 6) |
| 236 |
private int value; |
| 237 |
|
| 238 |
final private int[] alphabet; |
| 239 |
|
| 240 |
public Decoder(int flags, byte[] output) { |
| 241 |
this.output = output; |
| 242 |
|
| 243 |
alphabet = ((flags & URL_SAFE) == 0) ? DECODE : DECODE_WEBSAFE; |
| 244 |
state = 0; |
| 245 |
value = 0; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* @return an overestimate for the number of bytes {@code |
| 250 |
* len} bytes could decode to. |
| 251 |
*/ |
| 252 |
public int maxOutputSize(int len) { |
| 253 |
return len * 3/4 + 10; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Decode another block of input data. |
| 258 |
* |
| 259 |
* @return true if the state machine is still healthy. false if |
| 260 |
* bad base-64 data has been detected in the input stream. |
| 261 |
*/ |
| 262 |
public boolean process(byte[] input, int offset, int len, boolean finish) { |
| 263 |
if (this.state == 6) return false; |
| 264 |
|
| 265 |
int p = offset; |
| 266 |
len += offset; |
| 267 |
|
| 268 |
// Using local variables makes the decoder about 12% |
| 269 |
// faster than if we manipulate the member variables in |
| 270 |
// the loop. (Even alphabet makes a measurable |
| 271 |
// difference, which is somewhat surprising to me since |
| 272 |
// the member variable is final.) |
| 273 |
int state = this.state; |
| 274 |
int value = this.value; |
| 275 |
int op = 0; |
| 276 |
final byte[] output = this.output; |
| 277 |
final int[] alphabet = this.alphabet; |
| 278 |
|
| 279 |
while (p < len) { |
| 280 |
// Try the fast path: we're starting a new tuple and the |
| 281 |
// next four bytes of the input stream are all data |
| 282 |
// bytes. This corresponds to going through states |
| 283 |
// 0-1-2-3-0. We expect to use this method for most of |
| 284 |
// the data. |
| 285 |
// |
| 286 |
// If any of the next four bytes of input are non-data |
| 287 |
// (whitespace, etc.), value will end up negative. (All |
| 288 |
// the non-data values in decode are small negative |
| 289 |
// numbers, so shifting any of them up and or'ing them |
| 290 |
// together will result in a value with its top bit set.) |
| 291 |
// |
| 292 |
// You can remove this whole block and the output should |
| 293 |
// be the same, just slower. |
| 294 |
if (state == 0) { |
| 295 |
while (p+4 <= len && |
| 296 |
(value = ((alphabet[input[p] & 0xff] << 18) | |
| 297 |
(alphabet[input[p+1] & 0xff] << 12) | |
| 298 |
(alphabet[input[p+2] & 0xff] << 6) | |
| 299 |
(alphabet[input[p+3] & 0xff]))) >= 0) { |
| 300 |
output[op+2] = (byte) value; |
| 301 |
output[op+1] = (byte) (value >> 8); |
| 302 |
output[op] = (byte) (value >> 16); |
| 303 |
op += 3; |
| 304 |
p += 4; |
| 305 |
} |
| 306 |
if (p >= len) break; |
| 307 |
} |
| 308 |
|
| 309 |
// The fast path isn't available -- either we've read a |
| 310 |
// partial tuple, or the next four input bytes aren't all |
| 311 |
// data, or whatever. Fall back to the slower state |
| 312 |
// machine implementation. |
| 313 |
|
| 314 |
int d = alphabet[input[p++] & 0xff]; |
| 315 |
|
| 316 |
switch (state) { |
| 317 |
case 0: |
| 318 |
if (d >= 0) { |
| 319 |
value = d; |
| 320 |
++state; |
| 321 |
} else if (d != SKIP) { |
| 322 |
this.state = 6; |
| 323 |
return false; |
| 324 |
} |
| 325 |
break; |
| 326 |
|
| 327 |
case 1: |
| 328 |
if (d >= 0) { |
| 329 |
value = (value << 6) | d; |
| 330 |
++state; |
| 331 |
} else if (d != SKIP) { |
| 332 |
this.state = 6; |
| 333 |
return false; |
| 334 |
} |
| 335 |
break; |
| 336 |
|
| 337 |
case 2: |
| 338 |
if (d >= 0) { |
| 339 |
value = (value << 6) | d; |
| 340 |
++state; |
| 341 |
} else if (d == EQUALS) { |
| 342 |
// Emit the last (partial) output tuple; |
| 343 |
// expect exactly one more padding character. |
| 344 |
output[op++] = (byte) (value >> 4); |
| 345 |
state = 4; |
| 346 |
} else if (d != SKIP) { |
| 347 |
this.state = 6; |
| 348 |
return false; |
| 349 |
} |
| 350 |
break; |
| 351 |
|
| 352 |
case 3: |
| 353 |
if (d >= 0) { |
| 354 |
// Emit the output triple and return to state 0. |
| 355 |
value = (value << 6) | d; |
| 356 |
output[op+2] = (byte) value; |
| 357 |
output[op+1] = (byte) (value >> 8); |
| 358 |
output[op] = (byte) (value >> 16); |
| 359 |
op += 3; |
| 360 |
state = 0; |
| 361 |
} else if (d == EQUALS) { |
| 362 |
// Emit the last (partial) output tuple; |
| 363 |
// expect no further data or padding characters. |
| 364 |
output[op+1] = (byte) (value >> 2); |
| 365 |
output[op] = (byte) (value >> 10); |
| 366 |
op += 2; |
| 367 |
state = 5; |
| 368 |
} else if (d != SKIP) { |
| 369 |
this.state = 6; |
| 370 |
return false; |
| 371 |
} |
| 372 |
break; |
| 373 |
|
| 374 |
case 4: |
| 375 |
if (d == EQUALS) { |
| 376 |
++state; |
| 377 |
} else if (d != SKIP) { |
| 378 |
this.state = 6; |
| 379 |
return false; |
| 380 |
} |
| 381 |
break; |
| 382 |
|
| 383 |
case 5: |
| 384 |
if (d != SKIP) { |
| 385 |
this.state = 6; |
| 386 |
return false; |
| 387 |
} |
| 388 |
break; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if (!finish) { |
| 393 |
// We're out of input, but a future call could provide |
| 394 |
// more. |
| 395 |
this.state = state; |
| 396 |
this.value = value; |
| 397 |
this.op = op; |
| 398 |
return true; |
| 399 |
} |
| 400 |
|
| 401 |
// Done reading input. Now figure out where we are left in |
| 402 |
// the state machine and finish up. |
| 403 |
|
| 404 |
switch (state) { |
| 405 |
case 0: |
| 406 |
// Output length is a multiple of three. Fine. |
| 407 |
break; |
| 408 |
case 1: |
| 409 |
// Read one extra input byte, which isn't enough to |
| 410 |
// make another output byte. Illegal. |
| 411 |
this.state = 6; |
| 412 |
return false; |
| 413 |
case 2: |
| 414 |
// Read two extra input bytes, enough to emit 1 more |
| 415 |
// output byte. Fine. |
| 416 |
output[op++] = (byte) (value >> 4); |
| 417 |
break; |
| 418 |
case 3: |
| 419 |
// Read three extra input bytes, enough to emit 2 more |
| 420 |
// output bytes. Fine. |
| 421 |
output[op++] = (byte) (value >> 10); |
| 422 |
output[op++] = (byte) (value >> 2); |
| 423 |
break; |
| 424 |
case 4: |
| 425 |
// Read one padding '=' when we expected 2. Illegal. |
| 426 |
this.state = 6; |
| 427 |
return false; |
| 428 |
case 5: |
| 429 |
// Read all the padding '='s we expected and no more. |
| 430 |
// Fine. |
| 431 |
break; |
| 432 |
} |
| 433 |
|
| 434 |
this.state = state; |
| 435 |
this.op = op; |
| 436 |
return true; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// -------------------------------------------------------- |
| 441 |
// encoding |
| 442 |
// -------------------------------------------------------- |
| 443 |
|
| 444 |
/** |
| 445 |
* Base64-encode the given data and return a newly allocated |
| 446 |
* String with the result. |
| 447 |
* |
| 448 |
* @param input the data to encode |
| 449 |
* @param flags controls certain features of the encoded output. |
| 450 |
* Passing {@code DEFAULT} results in output that |
| 451 |
* adheres to RFC 2045. |
| 452 |
*/ |
| 453 |
public static String encodeToString(byte[] input, int flags) { |
| 454 |
try { |
| 455 |
return new String(encode(input, flags), "US-ASCII"); |
| 456 |
} catch (UnsupportedEncodingException e) { |
| 457 |
// US-ASCII is guaranteed to be available. |
| 458 |
throw new AssertionError(e); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Base64-encode the given data and return a newly allocated |
| 464 |
* String with the result. |
| 465 |
* |
| 466 |
* @param input the data to encode |
| 467 |
* @param offset the position within the input array at which to |
| 468 |
* start |
| 469 |
* @param len the number of bytes of input to encode |
| 470 |
* @param flags controls certain features of the encoded output. |
| 471 |
* Passing {@code DEFAULT} results in output that |
| 472 |
* adheres to RFC 2045. |
| 473 |
*/ |
| 474 |
public static String encodeToString(byte[] input, int offset, int len, int flags) { |
| 475 |
try { |
| 476 |
return new String(encode(input, offset, len, flags), "US-ASCII"); |
| 477 |
} catch (UnsupportedEncodingException e) { |
| 478 |
// US-ASCII is guaranteed to be available. |
| 479 |
throw new AssertionError(e); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Base64-encode the given data and return a newly allocated |
| 485 |
* byte[] with the result. |
| 486 |
* |
| 487 |
* @param input the data to encode |
| 488 |
* @param flags controls certain features of the encoded output. |
| 489 |
* Passing {@code DEFAULT} results in output that |
| 490 |
* adheres to RFC 2045. |
| 491 |
*/ |
| 492 |
public static byte[] encode(byte[] input, int flags) { |
| 493 |
return encode(input, 0, input.length, flags); |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Base64-encode the given data and return a newly allocated |
| 498 |
* byte[] with the result. |
| 499 |
* |
| 500 |
* @param input the data to encode |
| 501 |
* @param offset the position within the input array at which to |
| 502 |
* start |
| 503 |
* @param len the number of bytes of input to encode |
| 504 |
* @param flags controls certain features of the encoded output. |
| 505 |
* Passing {@code DEFAULT} results in output that |
| 506 |
* adheres to RFC 2045. |
| 507 |
*/ |
| 508 |
public static byte[] encode(byte[] input, int offset, int len, int flags) { |
| 509 |
Encoder encoder = new Encoder(flags, null); |
| 510 |
|
| 511 |
// Compute the exact length of the array we will produce. |
| 512 |
int output_len = len / 3 * 4; |
| 513 |
|
| 514 |
// Account for the tail of the data and the padding bytes, if any. |
| 515 |
if (encoder.do_padding) { |
| 516 |
if (len % 3 > 0) { |
| 517 |
output_len += 4; |
| 518 |
} |
| 519 |
} else { |
| 520 |
switch (len % 3) { |
| 521 |
case 0: break; |
| 522 |
case 1: output_len += 2; break; |
| 523 |
case 2: output_len += 3; break; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
// Account for the newlines, if any. |
| 528 |
if (encoder.do_newline && len > 0) { |
| 529 |
output_len += (((len-1) / (3 * Encoder.LINE_GROUPS)) + 1) * |
| 530 |
(encoder.do_cr ? 2 : 1); |
| 531 |
} |
| 532 |
|
| 533 |
encoder.output = new byte[output_len]; |
| 534 |
encoder.process(input, offset, len, true); |
| 535 |
|
| 536 |
assert encoder.op == output_len; |
| 537 |
|
| 538 |
return encoder.output; |
| 539 |
} |
| 540 |
|
| 541 |
/* package */ static class Encoder extends Coder { |
| 542 |
/** |
| 543 |
* Emit a new line every this many output tuples. Corresponds to |
| 544 |
* a 76-character line length (the maximum allowable according to |
| 545 |
* <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>). |
| 546 |
*/ |
| 547 |
public static final int LINE_GROUPS = 19; |
| 548 |
|
| 549 |
/** |
| 550 |
* Lookup table for turning Base64 alphabet positions (6 bits) |
| 551 |
* into output bytes. |
| 552 |
*/ |
| 553 |
private static final byte ENCODE[] = { |
| 554 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 555 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 556 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 557 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', |
| 558 |
}; |
| 559 |
|
| 560 |
/** |
| 561 |
* Lookup table for turning Base64 alphabet positions (6 bits) |
| 562 |
* into output bytes. |
| 563 |
*/ |
| 564 |
private static final byte ENCODE_WEBSAFE[] = { |
| 565 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 566 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 567 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 568 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', |
| 569 |
}; |
| 570 |
|
| 571 |
final private byte[] tail; |
| 572 |
/* package */ int tailLen; |
| 573 |
private int count; |
| 574 |
|
| 575 |
final public boolean do_padding; |
| 576 |
final public boolean do_newline; |
| 577 |
final public boolean do_cr; |
| 578 |
final private byte[] alphabet; |
| 579 |
|
| 580 |
public Encoder(int flags, byte[] output) { |
| 581 |
this.output = output; |
| 582 |
|
| 583 |
do_padding = (flags & NO_PADDING) == 0; |
| 584 |
do_newline = (flags & NO_WRAP) == 0; |
| 585 |
do_cr = (flags & CRLF) != 0; |
| 586 |
alphabet = ((flags & URL_SAFE) == 0) ? ENCODE : ENCODE_WEBSAFE; |
| 587 |
|
| 588 |
tail = new byte[2]; |
| 589 |
tailLen = 0; |
| 590 |
|
| 591 |
count = do_newline ? LINE_GROUPS : -1; |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* @return an overestimate for the number of bytes {@code |
| 596 |
* len} bytes could encode to. |
| 597 |
*/ |
| 598 |
public int maxOutputSize(int len) { |
| 599 |
return len * 8/5 + 10; |
| 600 |
} |
| 601 |
|
| 602 |
public boolean process(byte[] input, int offset, int len, boolean finish) { |
| 603 |
// Using local variables makes the encoder about 9% faster. |
| 604 |
final byte[] alphabet = this.alphabet; |
| 605 |
final byte[] output = this.output; |
| 606 |
int op = 0; |
| 607 |
int count = this.count; |
| 608 |
|
| 609 |
int p = offset; |
| 610 |
len += offset; |
| 611 |
int v = -1; |
| 612 |
|
| 613 |
// First we need to concatenate the tail of the previous call |
| 614 |
// with any input bytes available now and see if we can empty |
| 615 |
// the tail. |
| 616 |
|
| 617 |
switch (tailLen) { |
| 618 |
case 0: |
| 619 |
// There was no tail. |
| 620 |
break; |
| 621 |
|
| 622 |
case 1: |
| 623 |
if (p+2 <= len) { |
| 624 |
// A 1-byte tail with at least 2 bytes of |
| 625 |
// input available now. |
| 626 |
v = ((tail[0] & 0xff) << 16) | |
| 627 |
((input[p++] & 0xff) << 8) | |
| 628 |
(input[p++] & 0xff); |
| 629 |
tailLen = 0; |
| 630 |
}; |
| 631 |
break; |
| 632 |
|
| 633 |
case 2: |
| 634 |
if (p+1 <= len) { |
| 635 |
// A 2-byte tail with at least 1 byte of input. |
| 636 |
v = ((tail[0] & 0xff) << 16) | |
| 637 |
((tail[1] & 0xff) << 8) | |
| 638 |
(input[p++] & 0xff); |
| 639 |
tailLen = 0; |
| 640 |
} |
| 641 |
break; |
| 642 |
} |
| 643 |
|
| 644 |
if (v != -1) { |
| 645 |
output[op++] = alphabet[(v >> 18) & 0x3f]; |
| 646 |
output[op++] = alphabet[(v >> 12) & 0x3f]; |
| 647 |
output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 648 |
output[op++] = alphabet[v & 0x3f]; |
| 649 |
if (--count == 0) { |
| 650 |
if (do_cr) output[op++] = '\r'; |
| 651 |
output[op++] = '\n'; |
| 652 |
count = LINE_GROUPS; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
// At this point either there is no tail, or there are fewer |
| 657 |
// than 3 bytes of input available. |
| 658 |
|
| 659 |
// The main loop, turning 3 input bytes into 4 output bytes on |
| 660 |
// each iteration. |
| 661 |
while (p+3 <= len) { |
| 662 |
v = ((input[p] & 0xff) << 16) | |
| 663 |
((input[p+1] & 0xff) << 8) | |
| 664 |
(input[p+2] & 0xff); |
| 665 |
output[op] = alphabet[(v >> 18) & 0x3f]; |
| 666 |
output[op+1] = alphabet[(v >> 12) & 0x3f]; |
| 667 |
output[op+2] = alphabet[(v >> 6) & 0x3f]; |
| 668 |
output[op+3] = alphabet[v & 0x3f]; |
| 669 |
p += 3; |
| 670 |
op += 4; |
| 671 |
if (--count == 0) { |
| 672 |
if (do_cr) output[op++] = '\r'; |
| 673 |
output[op++] = '\n'; |
| 674 |
count = LINE_GROUPS; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
if (finish) { |
| 679 |
// Finish up the tail of the input. Note that we need to |
| 680 |
// consume any bytes in tail before any bytes |
| 681 |
// remaining in input; there should be at most two bytes |
| 682 |
// total. |
| 683 |
|
| 684 |
if (p-tailLen == len-1) { |
| 685 |
int t = 0; |
| 686 |
v = ((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 4; |
| 687 |
tailLen -= t; |
| 688 |
output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 689 |
output[op++] = alphabet[v & 0x3f]; |
| 690 |
if (do_padding) { |
| 691 |
output[op++] = '='; |
| 692 |
output[op++] = '='; |
| 693 |
} |
| 694 |
if (do_newline) { |
| 695 |
if (do_cr) output[op++] = '\r'; |
| 696 |
output[op++] = '\n'; |
| 697 |
} |
| 698 |
} else if (p-tailLen == len-2) { |
| 699 |
int t = 0; |
| 700 |
v = (((tailLen > 1 ? tail[t++] : input[p++]) & 0xff) << 10) | |
| 701 |
(((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 2); |
| 702 |
tailLen -= t; |
| 703 |
output[op++] = alphabet[(v >> 12) & 0x3f]; |
| 704 |
output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 705 |
output[op++] = alphabet[v & 0x3f]; |
| 706 |
if (do_padding) { |
| 707 |
output[op++] = '='; |
| 708 |
} |
| 709 |
if (do_newline) { |
| 710 |
if (do_cr) output[op++] = '\r'; |
| 711 |
output[op++] = '\n'; |
| 712 |
} |
| 713 |
} else if (do_newline && op > 0 && count != LINE_GROUPS) { |
| 714 |
if (do_cr) output[op++] = '\r'; |
| 715 |
output[op++] = '\n'; |
| 716 |
} |
| 717 |
|
| 718 |
assert tailLen == 0; |
| 719 |
assert p == len; |
| 720 |
} else { |
| 721 |
// Save the leftovers in tail to be consumed on the next |
| 722 |
// call to encodeInternal. |
| 723 |
|
| 724 |
if (p == len-1) { |
| 725 |
tail[tailLen++] = input[p]; |
| 726 |
} else if (p == len-2) { |
| 727 |
tail[tailLen++] = input[p]; |
| 728 |
tail[tailLen++] = input[p+1]; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
this.op = op; |
| 733 |
this.count = count; |
| 734 |
|
| 735 |
return true; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
private Base64() { } // don't instantiate |
| 740 |
} |