Android Base64 Byte Array Encode encodeLast1Byte(byte bytes[])

Here you can find the source of encodeLast1Byte(byte bytes[])

Description

encode Last Byte

Declaration

private static String encodeLast1Byte(byte bytes[]) 

Method Source Code

//package com.java2s;

public class Main {
    private static String encodeLast1Byte(byte bytes[]) {
        byte lastByte = bytes[bytes.length - 1];

        StringBuffer resultBuffer = new StringBuffer();

        byte bufferByte = 0;

        bufferByte = (byte) (lastByte >> 2);
        resultBuffer.append(encodeBase64Byte(bufferByte));

        bufferByte = (byte) ((lastByte & 0x03) << 4);
        resultBuffer.append(encodeBase64Byte(bufferByte));

        resultBuffer.append('=');
        resultBuffer.append('=');

        return resultBuffer.toString();
    }/*from  ww w  .  jav a  2 s .  com*/

    private static char encodeBase64Byte(byte base64Byte) {
        if ((base64Byte >= 0) && (base64Byte <= 25)) {
            return (char) (base64Byte + 65);
        } else if ((base64Byte >= 26) && (base64Byte <= 51)) {
            return (char) (base64Byte - 26 + 97);
        } else if ((base64Byte >= 52) && (base64Byte <= 61)) {
            return (char) (base64Byte - 52 + 48);
        } else if (base64Byte == 62) {
            return '+';
        } else if (base64Byte == 63) {
            return '/';
        } else {
            return '0';
        }
    }
}

Related

  1. encodeWebSafe(byte[] source, boolean doPadding)
  2. encode(byte[] bytes)
  3. encode(byte[] data)
  4. encodeBase64Byte(byte base64Byte)
  5. encodeLast2Bytes(byte bytes[])
  6. encode(byte[] data)
  7. encode(byte[] data)
  8. encode(byte raw[])
  9. encode(byte[] data)