Java String Encode encodeToString(String s)

Here you can find the source of encodeToString(String s)

Description

encode To String

License

Open Source License

Declaration

public static String encodeToString(String s) 

Method Source Code

//package com.java2s;
/**/* w  w w  . j  a  v  a 2  s . c  o  m*/
 * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved.
 *
 *   Author: Guoqiang Chen
 *    Email: subchen@gmail.com
 *   WebURL: https://github.com/subchen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
            .toCharArray();
    private static final String ENCODING = "UTF-8";

    public static String encodeToString(String s) {
        try {
            return new String(encodeToChar(s.getBytes(ENCODING), false));
        } catch (UnsupportedEncodingException ignore) {
            return null;
        }
    }

    public static String encodeToString(String s, boolean lineSep) {
        try {
            return new String(encodeToChar(s.getBytes(ENCODING), lineSep));
        } catch (UnsupportedEncodingException ignore) {
            return null;
        }
    }

    public static String encodeToString(byte[] arr) {
        return new String(encodeToChar(arr, false));
    }

    /**
     * Encodes a raw byte array into a BASE64 <code>String</code>.
     */
    public static String encodeToString(byte[] arr, boolean lineSep) {
        return new String(encodeToChar(arr, lineSep));
    }

    /**
     * Encodes a raw byte array into a BASE64 <code>char[]</code>.
     * @param lineSeparator optional CRLF after 76 chars, unless EOF.
     */
    public static char[] encodeToChar(byte[] arr, boolean lineSeparator) {
        int len = arr != null ? arr.length : 0;
        if (len == 0) {
            return new char[0];
        }

        int evenlen = (len / 3) * 3;
        int cnt = ((len - 1) / 3 + 1) << 2;
        int destLen = cnt + (lineSeparator ? (cnt - 1) / 76 << 1 : 0);
        char[] dest = new char[destLen];

        for (int s = 0, d = 0, cc = 0; s < evenlen;) {
            int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);

            dest[d++] = CHARS[(i >>> 18) & 0x3f];
            dest[d++] = CHARS[(i >>> 12) & 0x3f];
            dest[d++] = CHARS[(i >>> 6) & 0x3f];
            dest[d++] = CHARS[i & 0x3f];

            if (lineSeparator && (++cc == 19) && (d < (destLen - 2))) {
                dest[d++] = '\r';
                dest[d++] = '\n';
                cc = 0;
            }
        }

        int left = len - evenlen; // 0 - 2.
        if (left > 0) {
            int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);

            dest[destLen - 4] = CHARS[i >> 12];
            dest[destLen - 3] = CHARS[(i >>> 6) & 0x3f];
            dest[destLen - 2] = left == 2 ? CHARS[i & 0x3f] : '=';
            dest[destLen - 1] = '=';
        }
        return dest;
    }
}

Related

  1. encodeTexts(String s)
  2. encodeTextValue(char[] characters, int offset, int length, Writer writer)
  3. encodeThoroughly(String s)
  4. encodeToFlex(Object o)
  5. encodeToForm(Map msg)
  6. encodeURNComponent(String value)
  7. encodeUTF8(String input, boolean keepSpaces)
  8. encodeUTF8(String s)
  9. encodeUTF8(String s)