Android Base64 Byte Array Encode encodeBlock(byte raw[], int offset)

Here you can find the source of encodeBlock(byte raw[], int offset)

Description

encode Block

Declaration

protected static char[] encodeBlock(byte raw[], int offset) 

Method Source Code

//package com.java2s;

public class Main {
    protected static char[] encodeBlock(byte raw[], int offset) {
        int block = 0;
        int slack = raw.length - offset - 1;
        int end = slack < 2 ? slack : 2;

        for (int i = 0; i <= end; i++) {
            byte b = raw[offset + i];

            int neuter = b >= 0 ? ((int) (b)) : b + 256;
            block += neuter << 8 * (2 - i);
        }//from  w w  w.  j a va 2s  . c  o  m

        char base64[] = new char[4];

        for (int i = 0; i < 4; i++) {
            int sixbit = block >>> 6 * (3 - i) & 0x3f;
            base64[i] = getChar(sixbit);
        }

        if (slack < 1) {
            base64[2] = '=';
        }

        if (slack < 2) {
            base64[3] = '=';
        }

        return base64;
    }

    protected static char getChar(int sixbit) {
        if (sixbit >= 0 && sixbit <= 25) {
            return (char) (65 + sixbit);
        }

        if (sixbit >= 26 && sixbit <= 51) {
            return (char) (97 + (sixbit - 26));
        }

        if (sixbit >= 52 && sixbit <= 61) {
            return (char) (48 + (sixbit - 52));
        }

        if (sixbit == 62) {
            return '+';
        }

        return sixbit != 63 ? '?' : '/';
    }
}

Related

  1. encode(byte[] data)
  2. encode(byte[] data, int off, int len, Writer writer)
  3. encode(byte[] data)
  4. base64(byte[] target)
  5. toBase64(byte[] data)