Java Base64 Encode base64Encode(byte[] buf, int tw)

Here you can find the source of base64Encode(byte[] buf, int tw)

Description

Base64 encode a byte array, returning the returning string.

License

Open Source License

Parameter

Parameter Description
buf The byte array to encode.
tw The total length of any line, 0 for unlimited.

Return

buf encoded in Base64.

Declaration

public static final String base64Encode(byte[] buf, int tw) 

Method Source Code

//package com.java2s;
// under the terms of the GNU General Public License as published by the Free

public class Main {
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };
    /** Base-64 characters. */
    private static final String BASE_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    /** Base-64 padding character. */
    private static final char BASE_64_PAD = '=';

    /**// w ww .  ja v a  2s  .  c om
     * Base64 encode a byte array, returning the returning string.
     *
     * @param buf The byte array to encode.
     * @param tw  The total length of any line, 0 for unlimited.
     * @return <tt>buf</tt> encoded in Base64.
     */
    public static final String base64Encode(byte[] buf, int tw) {
        int srcLength = buf.length;
        byte[] input = new byte[3];
        int[] output = new int[4];
        StringBuffer out = new StringBuffer();
        int i = 0;
        int chars = 0;

        while (srcLength > 2) {
            input[0] = buf[i++];
            input[1] = buf[i++];
            input[2] = buf[i++];
            srcLength -= 3;

            output[0] = (input[0] & 0xff) >>> 2;
            output[1] = ((input[0] & 0x03) << 4) + ((input[1] & 0xff) >>> 4);
            output[2] = ((input[1] & 0x0f) << 2) + ((input[2] & 0xff) >>> 6);
            output[3] = input[2] & 0x3f;

            out.append(BASE_64.charAt(output[0]));
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
            out.append(BASE_64.charAt(output[1]));
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
            out.append(BASE_64.charAt(output[2]));
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
            out.append(BASE_64.charAt(output[3]));
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
        }

        if (srcLength != 0) {
            input[0] = input[1] = input[2] = 0;
            for (int j = 0; j < srcLength; j++)
                input[j] = buf[i + j];
            output[0] = (input[0] & 0xff) >>> 2;
            output[1] = ((input[0] & 0x03) << 4) + ((input[1] & 0xff) >>> 4);
            output[2] = ((input[1] & 0x0f) << 2) + ((input[2] & 0xff) >>> 6);

            out.append(BASE_64.charAt(output[0]));
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
            out.append(BASE_64.charAt(output[1]));
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
            if (srcLength == 1) {
                out.append(BASE_64_PAD);
            } else {
                out.append(BASE_64.charAt(output[2]));
            }
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
            out.append(BASE_64_PAD);
            if (tw > 0 && ++chars % tw == 0)
                out.append("\n");
        }
        if (tw > 0)
            out.append("\n");

        return out.toString();
    }

    /**
     * <p>Returns a string of hexadecimal digits from a byte array. Each byte is
     * converted to 2 hex symbols; zero(es) included.</p>
     *
     * <p>This method calls the method with same name and three arguments as:</p>
     *
     * <pre>
     *    toString(ba, 0, ba.length);
     * </pre>
     *
     * @param ba the byte array to convert.
     * @return a string of hexadecimal characters (two for each byte)
     * representing the designated input byte array.
     */
    public static String toString(byte[] ba) {
        return toString(ba, 0, ba.length);
    }

    /**
     * <p>Returns a string of hexadecimal digits from a byte array, starting at
     * <code>offset</code> and consisting of <code>length</code> bytes. Each byte
     * is converted to 2 hex symbols; zero(es) included.
     *
     * @param ba the byte array to convert.
     * @param offset the index from which to start considering the bytes to
     * convert.
     * @param length the count of bytes, starting from the designated offset to
     * convert.
     * @return a string of hexadecimal characters (two for each byte)
     * representing the designated input byte sub-array.
     */
    public static final String toString(byte[] ba, int offset, int length) {
        char[] buf = new char[length * 2];
        for (int i = 0, j = 0, k; i < length;) {
            k = ba[offset + i++];
            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
            buf[j++] = HEX_DIGITS[k & 0x0F];
        }
        return new String(buf);
    }

    /**
     * <p>Returns a string of hexadecimal digits from a byte array,
     * separating each byte with the specified character.</p>
     *
     * @param ba  The byte array to convert.
     * @param off From whence in the byte array to start.
     * @param len The number of bytes to convert.
     * @param sep The character to separate individual bytes with.
     * @return The string of hexadecimal characters, two per byte, with
     *    every third character being the separator character.
     */
    public static final String toString(byte[] ba, int off, int len, char sep) {
        char[] buf = new char[len * 3 - 1];
        for (int i = 0, j = 0, k; i < len;) {
            k = ba[off + i++];
            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
            buf[j++] = HEX_DIGITS[k & 0x0F];
            if (j < buf.length)
                buf[j++] = sep;
        }
        return new String(buf);
    }

    public static final String toString(byte[] ba, char sep) {
        return toString(ba, 0, ba.length, sep);
    }

    /**
     * <p>Returns a string of 8 hexadecimal digits (most significant digit first)
     * corresponding to the unsigned integer <code>n</code>.</p>
     *
     * @param n the unsigned integer to convert.
     * @return a hexadecimal string 8-character long.
     */
    public static String toString(int n) {
        char[] buf = new char[8];
        for (int i = 7; i >= 0; i--) {
            buf[i] = HEX_DIGITS[n & 0x0F];
            n >>>= 4;
        }
        return new String(buf);
    }

    /**
     * <p>Returns a string of hexadecimal digits from an integer array. Each int
     * is converted to 4 hex symbols.</p>
     */
    public static String toString(int[] ia) {
        int length = ia.length;
        char[] buf = new char[length * 8];
        for (int i = 0, j = 0, k; i < length; i++) {
            k = ia[i];
            buf[j++] = HEX_DIGITS[(k >>> 28) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 24) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
            buf[j++] = HEX_DIGITS[k & 0x0F];
        }
        return new String(buf);
    }

    /**
     * <p>Returns a string of 16 hexadecimal digits (most significant digit
     * first) corresponding to the unsigned long <code>n</code>.</p>
     *
     * @param n the unsigned long to convert.
     * @return a hexadecimal string 16-character long.
     */
    public static String toString(long n) {
        char[] b = new char[16];
        for (int i = 15; i >= 0; i--) {
            b[i] = HEX_DIGITS[(int) (n & 0x0FL)];
            n >>>= 4;
        }
        return new String(b);
    }
}

Related

  1. base64Encode(byte[] aData)
  2. base64Encode(byte[] aData)
  3. base64Encode(byte[] buf)
  4. base64Encode(byte[] bytes)
  5. base64encode(byte[] bytes)
  6. base64Encode(byte[] bytes)
  7. base64Encode(byte[] bytes)