Android Base64 Encode encode(String source)

Here you can find the source of encode(String source)

Description

Returns the base 64 encoded equivalent of a supplied string.

License

Open Source License

Parameter

Parameter Description
source the string to encode

Declaration

public static String encode(String source) 

Method Source Code

//package com.java2s;
/********************************************************************************************************************
 * $Id: Base64.java,v 1.4 2002/12/24 15:17:17 russgold Exp $
 *
 * Copyright (c) 2000-2002 by Russell Gold
 *
 * 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.//from   w w w  .  j  a v a  2  s  .c  o  m
 *
 * 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.
 *
 *******************************************************************************************************************/

public class Main {
    final static String encodingChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /**
     * Returns the base 64 encoded equivalent of a supplied string.
     * 
     * @param source
     *            the string to encode
     */
    public static String encode(String source) {
        char[] sourceBytes = getPaddedBytes(source);
        int numGroups = (sourceBytes.length + 2) / 3;
        char[] targetBytes = new char[4];
        char[] target = new char[4 * numGroups];

        for (int group = 0; group < numGroups; group++) {
            convert3To4(sourceBytes, group * 3, targetBytes);
            for (int i = 0; i < targetBytes.length; i++) {
                target[i + 4 * group] = encodingChar.charAt(targetBytes[i]);
            }
        }

        int numPadBytes = sourceBytes.length - source.length();

        for (int i = target.length - numPadBytes; i < target.length; i++)
            target[i] = '=';
        return new String(target);
    }

    private static char[] getPaddedBytes(String source) {
        char[] converted = source.toCharArray();
        int requiredLength = 3 * ((converted.length + 2) / 3);
        char[] result = new char[requiredLength];
        System.arraycopy(converted, 0, result, 0, converted.length);
        return result;
    }

    private static void convert3To4(char[] source, int sourceIndex,
            char[] target) {
        target[0] = (char) (source[sourceIndex] >>> 2);
        target[1] = (char) (((source[sourceIndex] & 0x03) << 4) | (source[sourceIndex + 1] >>> 4));
        target[2] = (char) (((source[sourceIndex + 1] & 0x0f) << 2) | (source[sourceIndex + 2] >>> 6));
        target[3] = (char) (source[sourceIndex + 2] & 0x3f);
    }
}

Related

  1. encodeBase64(String value)
  2. encode(String s)
  3. encode(final String str)
  4. encode(String str)
  5. encodedBase64(String currentString)
  6. toBase64(String value)
  7. toBase64(byte[] bytes)