Java String Encode encodeString(final String string)

Here you can find the source of encodeString(final String string)

Description

Encodes a string into Base64 format.

License

Open Source License

Parameter

Parameter Description
string a String to be encoded.

Return

A String with the Base64 encoded data.

Declaration

public static String encodeString(final String string) 

Method Source Code

//package com.java2s;
/**/* w  w  w  .  j a va2s .  c om*/
 * A Base64 Encoder/Decoder.
 * <p>
 * This class is used to encode and decode data in Base64 format as described in RFC 1521.
 * <p>
 * This is "Open Source" software and released under the
 * <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL </a>
 * license. <br>
 * It is provided "as is" without warranty of any kind. <br>
 * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.
 * <br>
 * Home page: <a href="http://www.source-code.biz">www.source-code.biz </a> <br>
 * <p>
 * Original name <b>Base64Coder.java </b>
 * <p>
 * Version history: <br>
 * 2003-07-22 Christian d'Heureuse (chdh): Module created. <br>
 * 2005-08-11 chdh: Lincense changed from GPL to LGPL. <br>
 * 2006-11-21 chdh: <br>
 * &nbsp; Method encode(String) renamed to encodeString(String). <br>
 * &nbsp; Method decode(String) renamed to decodeString(String). <br>
 * &nbsp; New method encode(byte[],int) added. <br>
 * &nbsp; New method decode(String) added. <br>
 * 2007-04-30 francis.naoum: Added to sfp_lib. <br>
 * &nbsp; byte[] encode(byte[]) changed to return a String. <br>
 *
 * @author Francis Naoum
 * @since 1.0.0
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Mapping table from 6-bit nibbles to Base64 characters.
     */
    private static final char[] MAP1 = new char[64];

    /**
     * Encodes a string into Base64 format. No blanks or line breaks are inserted.
     *
     * @param string a String to be encoded.
     * @return A String with the Base64 encoded data.
     */
    public static String encodeString(final String string) {
        String encodedString = null;

        try {
            encodedString = encodeString(string, "UTF-8");
        } catch (UnsupportedEncodingException uue) {
            // Should never happen, java has to support "UTF-8".
        }

        return encodedString;
    }

    /**
     * Encodes a string into Base64 format. No blanks or line breaks are inserted.
     *
     * @param string a String to be encoded.
     * @param encoding The character encoding of the string.
     * @return A String with the Base64 encoded data.
     *
     * @throws UnsupportedEncodingException if the java runtime does not support <code>encoding</code>.
     */
    public static String encodeString(final String string, final String encoding)
            throws UnsupportedEncodingException {
        byte[] stringBytes = string.getBytes(encoding);
        return encode(stringBytes);
    }

    /**
     * Encodes a byte array into Base64 format. No blanks or line breaks are inserted.
     * <p>
     * This method has been modified to return a string not a char[].
     *
     * @param input an array containing the data bytes to be encoded.
     * @return A character array with the Base64 encoded data.
     */
    public static String encode(final byte[] input) {
        char[] chars = encode(input, input.length);
        return new String(chars);
    }

    /**
     * Encodes a byte array into Base64 format. No blanks or line breaks are inserted.
     *
     * @param input an array containing the data bytes to be encoded.
     * @param iLen number of bytes to process in <code>in</code>.
     * @return A character array with the Base64 encoded data.
     */
    public static char[] encode(final byte[] input, final int iLen) {
        int oDataLen = (iLen * 4 + 2) / 3; // output length without padding
        int oLen = ((iLen + 2) / 3) * 4; // output length including padding
        char[] out = new char[oLen];
        int ip = 0;
        int op = 0;

        while (ip < iLen) {
            int i0 = input[ip++] & 0xff;
            int i1 = ip < iLen ? input[ip++] & 0xff : 0;
            int i2 = ip < iLen ? input[ip++] & 0xff : 0;
            int o0 = i0 >>> 2;
            int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
            int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
            int o3 = i2 & 0x3F;
            out[op++] = MAP1[o0];
            out[op++] = MAP1[o1];
            out[op] = op < oDataLen ? MAP1[o2] : '=';
            op++;
            out[op] = op < oDataLen ? MAP1[o3] : '=';
            op++;
        }

        return out;
    }
}

Related

  1. encodeSpecialMdxCharactersInNames(String name)
  2. encodeState(Map params)
  3. encodeStr(String str)
  4. encodeStr(String str)
  5. encodeStr(String t)
  6. encodeString(OutputStream out, String str)
  7. encodeString(String b)
  8. encodeString(String in)
  9. encodeString(String myString)