Inline Base64#decode4to3

Relates #16725
This commit is contained in:
Jason Tedor 2016-02-12 14:39:52 -05:00
parent a1740d6661
commit cf1d9cfefc
1 changed files with 23 additions and 54 deletions

View File

@ -942,17 +942,10 @@ public final class Base64 {
* or there is not enough room in the array. * or there is not enough room in the array.
* @since 1.3 * @since 1.3
*/ */
private static int decode4to3( private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset, int options) {
byte[] source, int srcOffset,
byte[] destination, int destOffset, int options) {
// Lots of error checking and exception throwing // Lots of error checking and exception throwing
if (source == null) { Objects.requireNonNull(source, "Source array was null.");
throw new NullPointerException("Source array was null."); Objects.requireNonNull(destination, "Destination array was null.");
} // end if
if (destination == null) {
throw new NullPointerException("Destination array was null.");
} // end if
if (srcOffset < 0 || srcOffset + 3 >= source.length) { if (srcOffset < 0 || srcOffset + 3 >= source.length) {
throw new IllegalArgumentException(String.format(Locale.ROOT, throw new IllegalArgumentException(String.format(Locale.ROOT,
"Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset)); "Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset));
@ -962,56 +955,36 @@ public final class Base64 {
"Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset)); "Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset));
} // end if } // end if
byte[] DECODABET = getDecodabet(options); byte[] DECODABET = getDecodabet(options);
// Two ways to do the same thing. Don't know which way I like best.
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
// | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12);
destination[destOffset] = (byte) (outBuff >>> 16);
// Example: Dk== // Example: Dk==
if (source[srcOffset + 2] == EQUALS_SIGN) { if (source[srcOffset + 2] == EQUALS_SIGN) {
// Two ways to do the same thing. Don't know which way I like best.
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
// | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12);
destination[destOffset] = (byte) (outBuff >>> 16);
return 1; return 1;
} }
// Example: DkL= outBuff |= ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6);
else if (source[srcOffset + 3] == EQUALS_SIGN) { destination[destOffset + 1] = (byte) (outBuff >>> 8);
// Two ways to do the same thing. Don't know which way I like best.
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
// | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
// | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
| ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6);
destination[destOffset] = (byte) (outBuff >>> 16); // Example: DkL=
destination[destOffset + 1] = (byte) (outBuff >>> 8); if (source[srcOffset + 3] == EQUALS_SIGN) {
return 2; return 2;
} }
outBuff |= ((DECODABET[source[srcOffset + 3]] & 0xFF));
destination[destOffset + 2] = (byte) (outBuff);
// Example: DkLE // Example: DkLE
else { return 3;
// Two ways to do the same thing. Don't know which way I like best. }
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
// | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
// | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )
// | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
| ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6)
| ((DECODABET[source[srcOffset + 3]] & 0xFF));
destination[destOffset] = (byte) (outBuff >> 16);
destination[destOffset + 1] = (byte) (outBuff >> 8);
destination[destOffset + 2] = (byte) (outBuff);
return 3;
}
} // end decodeToBytes
/** /**
@ -1056,13 +1029,9 @@ public final class Base64 {
* @throws java.io.IOException If bogus characters exist in source data * @throws java.io.IOException If bogus characters exist in source data
* @since 1.3 * @since 1.3
*/ */
public static byte[] decode(byte[] source, int off, int len, int options) public static byte[] decode(byte[] source, int off, int len, int options) throws java.io.IOException {
throws java.io.IOException {
// Lots of error checking and exception throwing // Lots of error checking and exception throwing
if (source == null) { Objects.requireNonNull(source, "Cannot decode null source array.");
throw new NullPointerException("Cannot decode null source array.");
} // end if
if (off < 0 || off + len > source.length) { if (off < 0 || off + len > source.length) {
throw new IllegalArgumentException(String.format(Locale.ROOT, throw new IllegalArgumentException(String.format(Locale.ROOT,
"Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len)); "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len));