Android Byte Array Encode encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, byte[] alphabet)

Here you can find the source of encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, byte[] alphabet)

Description

Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination.

License

Apache License

Parameter

Parameter Description
source the array to convert
srcOffset the index where conversion begins
numSigBytes the number of significant bytes in your array
destination the array to hold the conversion
destOffset the index where output will be put
alphabet is the encoding alphabet

Return

the destination array

Declaration

private static byte[] encode3to4(byte[] source, int srcOffset,
        int numSigBytes, byte[] destination, int destOffset,
        byte[] alphabet) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /** The equals sign (=) as a byte. */
    private final static byte EQUALS_SIGN = (byte) '=';

    /**/*from   www  .j  a  va 2  s .co m*/
     * Encodes up to three bytes of the array <var>source</var>
     * and writes the resulting four Base64 bytes to <var>destination</var>.
     * The source and destination arrays can be manipulated
     * anywhere along their length by specifying
     * <var>srcOffset</var> and <var>destOffset</var>.
     * This method does not check to make sure your arrays
     * are large enough to accommodate <var>srcOffset</var> + 3 for
     * the <var>source</var> array or <var>destOffset</var> + 4 for
     * the <var>destination</var> array.
     * The actual number of significant bytes in your array is
     * given by <var>numSigBytes</var>.
     *
     * @param source the array to convert
     * @param srcOffset the index where conversion begins
     * @param numSigBytes the number of significant bytes in your array
     * @param destination the array to hold the conversion
     * @param destOffset the index where output will be put
     * @param alphabet is the encoding alphabet
     * @return the <var>destination</var> array
     * @since 1.3
     */
    private static byte[] encode3to4(byte[] source, int srcOffset,
            int numSigBytes, byte[] destination, int destOffset,
            byte[] alphabet) {
        //           1         2         3
        // 01234567890123456789012345678901 Bit position
        // --------000000001111111122222222 Array position from threeBytes
        // --------|    ||    ||    ||    | Six bit groups to index alphabet
        //          >>18  >>12  >> 6  >> 0  Right shift necessary
        //                0x3f  0x3f  0x3f  Additional AND

        // Create buffer with zero-padding if there are only one or two
        // significant bytes passed in the array.
        // We have to shift left 24 in order to flush out the 1's that appear
        // when Java treats a value as negative that is cast from a byte to an int.
        int inBuff = (numSigBytes > 0 ? ((source[srcOffset] << 24) >>> 8)
                : 0)
                | (numSigBytes > 1 ? ((source[srcOffset + 1] << 24) >>> 16)
                        : 0)
                | (numSigBytes > 2 ? ((source[srcOffset + 2] << 24) >>> 24)
                        : 0);

        switch (numSigBytes) {
        case 3:
            destination[destOffset] = alphabet[(inBuff >>> 18)];
            destination[destOffset + 1] = alphabet[(inBuff >>> 12) & 0x3f];
            destination[destOffset + 2] = alphabet[(inBuff >>> 6) & 0x3f];
            destination[destOffset + 3] = alphabet[(inBuff) & 0x3f];
            return destination;
        case 2:
            destination[destOffset] = alphabet[(inBuff >>> 18)];
            destination[destOffset + 1] = alphabet[(inBuff >>> 12) & 0x3f];
            destination[destOffset + 2] = alphabet[(inBuff >>> 6) & 0x3f];
            destination[destOffset + 3] = EQUALS_SIGN;
            return destination;
        case 1:
            destination[destOffset] = alphabet[(inBuff >>> 18)];
            destination[destOffset + 1] = alphabet[(inBuff >>> 12) & 0x3f];
            destination[destOffset + 2] = EQUALS_SIGN;
            destination[destOffset + 3] = EQUALS_SIGN;
            return destination;
        default:
            return destination;
        } // end switch
    }
}

Related

  1. encode(byte[] source, int off, int len, byte[] alphabet, int maxLineLength)
  2. encode(byte[] source, int off, int len, byte[] alphabet, int maxLineLength)
  3. encode(byte[] source, int off, int len, byte[] alphabet, int maxLineLength)
  4. encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, byte[] alphabet)
  5. encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, byte[] alphabet)
  6. encodeToString(byte[] input, int flags)
  7. encodeToString(byte[] input, int offset, int len, int flags)
  8. encodeWebSafe(byte[] source, boolean doPadding)
  9. encodeWebSafe(byte[] source, boolean doPadding)