Android UTF8 Encode getStringInUtf8(final String str)

Here you can find the source of getStringInUtf8(final String str)

Description

get String In Utf

License

Apache License

Declaration

public final static byte[] getStringInUtf8(final String str) 

Method Source Code

//package com.java2s;
/**//from   w ww .ja v  a  2 s  .c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */

public class Main {
    public final static byte[] getStringInUtf8(final String str) {
        final int length = str.length();
        boolean expanded = false;
        byte[] result = new byte[length];
        int i = 0;
        int out = 0;
        char c;
        while (i < length) {
            c = str.charAt(i++);
            if (c < 0x80) {
                result[out++] = (byte) c;
                continue;
            }
            if ((c >= 0xD800 && c <= 0xDBFF)
                    || (c >= 0xDC00 && c <= 0xDFFF)) {
                //No Surrogates in sun java
                result[out++] = 0x3f;
                continue;
            }
            if (!expanded) {
                byte newResult[] = new byte[3 * length];
                System.arraycopy(result, 0, newResult, 0, out);
                result = newResult;
                expanded = true;
            }
            char ch;
            int bias;
            byte write;
            if (c > 0x07FF) {
                ch = (char) (c >>> 12);
                write = (byte) 0xE0;
                if (ch > 0) {
                    write |= (ch & 0x0F);
                }
                result[out++] = write;
                write = (byte) 0x80;
                bias = 0x3F;
            } else {
                write = (byte) 0xC0;
                bias = 0x1F;
            }
            ch = (char) (c >>> 6);
            if (ch > 0) {
                write |= (ch & bias);
            }
            result[out++] = write;
            result[out++] = (byte) (0x80 | ((c) & 0x3F));
        }
        if (expanded) {
            byte newResult[] = new byte[out];
            System.arraycopy(result, 0, newResult, 0, out);
            result = newResult;
        }
        return result;
    }
}

Related

  1. bytes2StringUTF8(byte[] buf)
  2. bytes2StringUTF8(byte[] buf, int bufOffset, int bufLength, boolean bigEndian)
  3. bytes2charsUTF8(byte[] buf, int bufOffset, int bufLength, char[] cbuf, boolean bigEndian)
  4. bytesUTF8len(byte[] buf, int bufOffset, int bufLength)
  5. char2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag)
  6. stringToUtf8Bytes(String string)
  7. utf8encode(String str)
  8. isValidUTF8(byte[] input)
  9. string2BytesUTF8(String str)