Java Base64 Encode base64Encode(String _s, String _enc)

Here you can find the source of base64Encode(String _s, String _enc)

Description

base 64 encoding, string converted to bytes using specified encoding

License

Open Source License

Parameter

Parameter Description
String encoding, can be null, then iso-8859-1 used

Exception

Parameter Description
no exceptions

Return

String result of encoding as iso-8859-1 string
return null in case of invalid encoding or original string null

Declaration

public final static String base64Encode(String _s, String _enc) 

Method Source Code

//package com.java2s;

public class Main {
    public final static String ISO_8859_1 = "ISO-8859-1";
    protected final static char BASE64ARRAY[] = { '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', '+', '/' };

    /**//from  w w  w.j  a va2s .c  o  m
     * base 64 encoding, string converted to bytes using specified encoding
     *
     * @param String <val>_s</val> original string to encode
     * @param String encoding, can be null, then iso-8859-1 used
     * @return String result of encoding as iso-8859-1 string<br>
     *         return null in case of invalid encoding or original string null
     * @throws no exceptions
     */
    public final static String base64Encode(String _s, String _enc) {
        if (_s == null)
            return null;
        if (_enc == null)
            _enc = ISO_8859_1;
        try {
            return base64Encode(_s.getBytes(_enc));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base 64 encoding, array of bytes converted to bytes using specified encoding
     *
     * @param String <val>_s</val> original string to encode
     * @param String encoding, can be null, then iso-8859-1 used
     * @return String result of encoding as iso-8859-1 string<br>
     * @throws <code>NullPointerException</code>
     *          if input parameter is null
     */
    public final static String base64Encode(byte[] _bytes) {
        StringBuffer encodedBuffer = new StringBuffer((int) (_bytes.length * 1.5));
        int i = 0;
        int pad = 0;
        while (i < _bytes.length) {
            int b1 = (0xFF & _bytes[i++]);
            int b2;
            int b3;
            if (i >= _bytes.length) {
                b2 = 0;
                b3 = 0;
                pad = 2;
            } else {
                b2 = 0xFF & _bytes[i++];
                if (i >= _bytes.length) {
                    b3 = 0;
                    pad = 1;
                } else
                    b3 = (0xFF & _bytes[i++]);
            }
            byte c1 = (byte) (b1 >> 2);
            byte c2 = (byte) (((b1 & 0x3) << 4) | (b2 >> 4));
            byte c3 = (byte) (((b2 & 0xf) << 2) | (b3 >> 6));
            byte c4 = (byte) (b3 & 0x3f);
            encodedBuffer.append(BASE64ARRAY[c1]).append(BASE64ARRAY[c2]);
            switch (pad) {
            case 0:
                encodedBuffer.append(BASE64ARRAY[c3]).append(BASE64ARRAY[c4]);
                break;
            case 1:
                encodedBuffer.append(BASE64ARRAY[c3]).append('=');
                break;
            case 2:
                encodedBuffer.append("==");
                break;
            }
        }
        return encodedBuffer.toString();
    }
}

Related

  1. base64Encode(byte[] in)
  2. Base64Encode(byte[] input, boolean addLineBreaks)
  3. base64Encode(byte[] param)
  4. base64encode(final byte[] data)
  5. base64Encode(int value)
  6. base64Encode(String plaintext)
  7. base64Encode(String plainTextString)
  8. base64Encode(String s)
  9. base64encode(String s)