| 561 | } |
| 562 | |
| 563 | public boolean process(byte[] input, int offset, int len, boolean finish) { |
| 564 | // Using local variables makes the encoder about 9% faster. |
| 565 | final byte[] alphabet = this.alphabet; |
| 566 | final byte[] output = this.output; |
| 567 | int op = 0; |
| 568 | int count = this.count; |
| 569 | |
| 570 | int p = offset; |
| 571 | len += offset; |
| 572 | int v = -1; |
| 573 | |
| 574 | // First we need to concatenate the tail of the previous call |
| 575 | // with any input bytes available now and see if we can empty |
| 576 | // the tail. |
| 577 | |
| 578 | switch (tailLen) { |
| 579 | case 0: |
| 580 | // There was no tail. |
| 581 | break; |
| 582 | |
| 583 | case 1: |
| 584 | if (p + 2 <= len) { |
| 585 | // A 1-byte tail with at least 2 bytes of |
| 586 | // input available now. |
| 587 | v = ((tail[0] & 0xff) << 16) | |
| 588 | ((input[p++] & 0xff) << 8) | |
| 589 | (input[p++] & 0xff); |
| 590 | tailLen = 0; |
| 591 | } |
| 592 | break; |
| 593 | |
| 594 | case 2: |
| 595 | if (p + 1 <= len) { |
| 596 | // A 2-byte tail with at least 1 byte of input. |
| 597 | v = ((tail[0] & 0xff) << 16) | |
| 598 | ((tail[1] & 0xff) << 8) | |
| 599 | (input[p++] & 0xff); |
| 600 | tailLen = 0; |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | if (v != -1) { |
| 606 | output[op++] = alphabet[(v >> 18) & 0x3f]; |
| 607 | output[op++] = alphabet[(v >> 12) & 0x3f]; |
| 608 | output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 609 | output[op++] = alphabet[v & 0x3f]; |
| 610 | if (--count == 0) { |
| 611 | if (do_cr) output[op++] = '\r'; |
| 612 | output[op++] = '\n'; |
| 613 | count = LINE_GROUPS; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // At this point either there is no tail, or there are fewer |
| 618 | // than 3 bytes of input available. |
| 619 | |
| 620 | // The main loop, turning 3 input bytes into 4 output bytes on |