Java Base64 Encode base64encode(byte[] bytes)

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

Description

baseencode

License

Open Source License

Declaration

public static String base64encode(byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final char[] b64table = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
            '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

    public static String base64encode(byte[] bytes) {
        int current = 0;
        int state = 0;

        StringBuilder sb = new StringBuilder();

        for (byte b : bytes) {
            switch (state) {
            case 0:
                sb.append(b64table[(b & 0b11111100) >>> 2]);
                current = (b & 0b00000011) << 4;
                state = 1;/*  w ww .java 2s .  c o  m*/
                break;
            case 1:
                current = current ^ ((b & 0b11110000) >>> 4);
                sb.append(b64table[current]);
                current = (b & 0b00001111) << 2;
                state = 2;
                break;
            case 2:
                current = current ^ ((b & 0b11000000) >>> 6);
                sb.append(b64table[current]);
                sb.append(b64table[b & 0b00111111]);
                state = 0;
                break;
            }
        }

        // explicit padding unnecessary
        if (state > 0) {
            sb.append(b64table[current]);
        }

        return sb.toString();
    }
}

Related

  1. base64Encode(byte[] buf)
  2. base64Encode(byte[] buf, int tw)
  3. base64Encode(byte[] bytes)
  4. base64Encode(byte[] bytes)
  5. base64Encode(byte[] bytes)
  6. base64Encode(byte[] data)
  7. base64encode(byte[] data)
  8. base64Encode(byte[] data)
  9. base64Encode(byte[] in)