Java Encode encodeStringBase64(String str)

Here you can find the source of encodeStringBase64(String str)

Description

Encodes a given string to Base64.

License

Open Source License

Parameter

Parameter Description
str String.

Return

Base64 string.

Declaration

public static String encodeStringBase64(String str) 

Method Source Code

//package com.java2s;
/* Util.java//from w  w w. j  ava2  s  . c o  m
 * 
 * Networking ME
 * Copyright (c) 2013 eMob Tech (http://www.emobtech.com/)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * <p>
     * UTF-8 encoding.
     * </p>
     */
    public static final String UTF8 = "UTF-8";

    /**
     * <p>
     * Encodes a given string to Base64.
     * </p>
     * @param str String.
     * @return Base64 string.
     */
    public static String encodeStringBase64(String str) {
        if (isEmptyString(str)) {
            return str;
        }
        //
        final String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        //
        byte[] code = c.getBytes();
        byte[] s = str.getBytes();
        int x;
        int y = str.length() - (str.length() % 3);
        byte[] coded = new byte[4];
        StringBuffer dest = new StringBuffer();
        //
        for (x = 0; x < y; x += 3) {
            coded[3] = code[s[x + 2] % 64];
            coded[0] = code[s[x] >> 2];
            coded[1] = new Integer((s[x] % 4) << 4).byteValue();
            coded[1] += s[x + 1] >> 4;
            coded[1] = code[coded[1]];
            coded[2] = new Integer((s[x + 1] % 16) << 2).byteValue();
            coded[2] += s[x + 2] / 64;
            coded[2] = code[coded[2]];
            //
            dest.append(new String(coded));
        }
        //
        x = y;
        //
        if (s.length % 3 == 0) {
            return dest.toString();
        }
        //
        if (s.length % 3 == 1) {
            coded[2] = '=';
            coded[3] = '=';
            coded[0] = code[s[x] >> 2];
            coded[1] = code[new Integer((s[x] % 4) << 4).byteValue()];
            //
            dest.append(new String(coded));
        }
        //
        if (s.length % 3 == 2) {
            coded[3] = '=';
            coded[0] = code[s[x] >> 2];
            coded[1] = new Integer((s[x] % 4) << 4).byteValue();
            coded[1] += s[x + 1] >> 4;
            coded[1] = code[coded[1]];
            coded[2] = code[new Integer((s[x + 1] % 16) << 2).byteValue()];
            //
            dest.append(new String(coded));
        }
        //
        return dest.toString();
    }

    /**
     * <p>
     * Checks whether the given string is null or empty.
     * </p>
     * @param str The string.
     * @return true null/empty.
     */
    public static boolean isEmptyString(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * <p>
     * Returns the string of a given bytes.
     * </p>
     * @param bytes Bytes.
     * @return String.
     */
    public static String toString(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }
        //
        try {
            return new String(bytes, UTF8);
        } catch (UnsupportedEncodingException e) {
            return new String(bytes);
        }
    }
}

Related

  1. encodePart(final String part, final String charset, final BitSet allowed)
  2. encodePersonal( InternetAddress[] addresses)
  3. encodeQP(String text)
  4. encodeResult(final Object result)
  5. encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out)
  6. encodeTagChar(char c, Appendable buffer)
  7. encodeToTargetEncoding(String text, String sourceEncoding, String targetEncoding)
  8. encodeUint(OutputStream out, final long value)
  9. encodeUInt32(int value, OutputStream out)