Android Byte Array to UTF8 String Convert utf8String(byte[] array, int start)

Here you can find the source of utf8String(byte[] array, int start)

Description

Convert the array into a modified UTF-8 Java string.

Declaration

public static String utf8String(byte[] array, int start) 

Method Source Code

import java.util.Random;
import x.android.defs.ERROR;

public class Main{
    /**/*from w w w. ja  v  a  2  s .  c  o m*/
     * Convert the array into a modified UTF-8 Java string.
     * \param array Byte array with data to be parsed.
     * \param start The index of the first byte in the array.
     * \remarks The convertion takes the first two bytes as the number of
     * bytes to read. The following sequence must be a valid UTF-8 java
     * encoded string.
     * \return A string with the value parsed or \b null if an error is
     * encontered.
     **/
    public static String utf8String(byte[] array, int start) {
        int limit = arrays.length(array) - start;
        if (limit <= 2)
            return null; /* Impossible conversion. */

        int count = 0x0000FFFF & arrays.bigEndToShort(array, start);
        if (count > limit)
            return null; /* Invalid length. */

        int i = start, c = 0;
        byte temp;
        char[] data;

        limit = arrays.length(array);

        /* We alloc memory previously to gain performance. */
        data = new char[count];

        while (i < limit) {
            /* First bit is 0 or 1: single byte char. */
            if ((array[i] & 0x80) == 0x00)
                data[c] = (char) (array[i] & 0xFF);

            /* First three bits are 110: double byte character. */
            else if ((array[i] & 0xC0) == 0xC0) {
                data[c] = (char) (((array[i] & 0x1F) << 6) | (array[i + 1] & 0x3F));
                i++;
            }

            /* First four bits are 1110: three byte character. */
            else if ((array[i] & 0xE0) == 0xE0) {
                data[c] = (char) (((array[i] & 0x0F) << 12)
                        | ((array[i + 1] & 0x3F) << 6) | (array[i + 2] & 0x3F));
                i += 2;
            }

            /* Otherwise this is a bad encoded string. */
            else
                return null;

            i++;
            c++;
        }

        try {
            return new String(data, 0, c);
        } catch (Exception ex) {
            return null;
        }
    }
    /**
     * Safely gets the length of an array.
     * \param array The array thats the length should be retrieved.
     * \returns The length of the array. If \a array is \b null the return will
     * be zero.
     **/
    public static int length(byte[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(char[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(short[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(int[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(long[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(String[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(Object[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * Converts a byte array into short from big-endian format.
     * \param array The byte array to convert from.
     * \param start The starting point, int the array \a a, where the
     * conversion should start.
     * \returns The number after the conversion.
     **/
    public static short bigEndToShort(byte[] array, int start) {
        return (short) ((array[start + 0] << 8) | (array[start + 1] & 0xFF));
    }
}