silverbullet-common/silverbullet-utils/src/main/java/com/silverbullet/utils/Base64.java
/**
     * Encodes a raw byte array into a BASE64 <code>byte[]</code>.
     */
Encodes a raw byte array into a BASE64 byte[].
Encodes a raw byte array into a BASE64 byte [] .
public static byte[] encodeToByte(byte[] arr, boolean lineSep) {
        int len = arr != null ? arr.length : 0;
        if (len == 0) {
            return new byte[0];
        }
        int evenlen = (len / 3) * 3;
        int cnt = ((len - 1) / 3 + 1) << 2;
        int destlen = cnt + (lineSep ? (cnt - 1) / 76 << 1 : 0);
        byte[] dest = new byte[destlen];
        for (int s = 0, d = 0, cc = 0; s < evenlen;) {
            int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8
                    | (arr[s++] & 0xff);
            dest[d++] = (byte) CHARS[(i >>> 18) & 0x3f];
            dest[d++] = (byte) CHARS[(i >>> 12) & 0x3f];
            dest[d++] = (byte) CHARS[(i >>> 6) & 0x3f];
            dest[d++] = (byte) CHARS[i & 0x3f];
            if (lineSep && ++cc == 19 && d < destlen - 2) {
                dest[d++] = '\r';
                dest[d++] = '\n';
                cc = 0;
            }
        }
        int left = len - evenlen;
        if (left > 0) {
            int i = ((arr[evenlen] & 0xff) << 10)
                    | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);
            dest[destlen - 4] = (byte) CHARS[i >> 12];
            dest[destlen - 3] = (byte) CHARS[(i >>> 6) & 0x3f];
            dest[destlen - 2] = left == 2 ? (byte) CHARS[i & 0x3f] : (byte) '=';
            dest[destlen - 1] = '=';
        }
        return dest;
    }
public static byte [ ] encodeToByte ( byte [ ] arr , boolean lineSep ) { int len = arr != null ? arr . length : 0 ; if ( len == 0 ) { return new byte [ 0 ] ; } int evenlen = ( len / 3 ) * 3 ; int cnt = ( ( len - 1 ) / 3 + 1 ) << 2 ; int destlen = cnt + ( lineSep ? ( cnt - 1 ) / 76 << 1 : 0 ) ; byte [ ] dest = new byte [ destlen ] ; for ( int s = 0 , d = 0 , cc = 0 ; s < evenlen ; ) { int i = ( arr [ s ++ ] & 0xff ) << 16 | ( arr [ s ++ ] & 0xff ) << 8 | ( arr [ s ++ ] & 0xff ) ; dest [ d ++ ] = ( byte ) CHARS [ ( i >>> 18 ) & 0x3f ] ; dest [ d ++ ] = ( byte ) CHARS [ ( i >>> 12 ) & 0x3f ] ; dest [ d ++ ] = ( byte ) CHARS [ ( i >>> 6 ) & 0x3f ] ; dest [ d ++ ] = ( byte ) CHARS [ i & 0x3f ] ; if ( lineSep && ++ cc == 19 && d < destlen - 2 ) { dest [ d ++ ] = '\r' ; dest [ d ++ ] = '\n' ; cc = 0 ; } } int left = len - evenlen ; if ( left > 0 ) { int i = ( ( arr [ evenlen ] & 0xff ) << 10 ) | ( left == 2 ? ( ( arr [ len - 1 ] & 0xff ) << 2 ) : 0 ) ; dest [ destlen - 4 ] = ( byte ) CHARS [ i >> 12 ] ; dest [ destlen - 3 ] = ( byte ) CHARS [ ( i >>> 6 ) & 0x3f ] ; dest [ destlen - 2 ] = left == 2 ? ( byte ) CHARS [ i & 0x3f ] : ( byte ) '=' ; dest [ destlen - 1 ] = '=' ; } return dest ; }
Encodes a raw byte array into a BASE64 <code>byte[]</code>.
Encodes a raw byte array into a BASE64 <code > byte [] < / code > .