Java UTF from toUTF8(byte[] outputBuffer, String string, char[] workingBuffer)

Here you can find the source of toUTF8(byte[] outputBuffer, String string, char[] workingBuffer)

Description

to UTF

License

Open Source License

Declaration

public static int toUTF8(byte[] outputBuffer, String string, char[] workingBuffer) 

Method Source Code

//package com.java2s;

public class Main {
    final static char[] DigitTens = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1',
            '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3',
            '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5',
            '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7',
            '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9',
            '9', };
    final static char[] DigitOnes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', };
    final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    public static int toUTF8(byte[] outputBuffer, short number) {
        return toUTF8(outputBuffer, (long) number);
    }/*from   ww  w. j  a  v a 2s  .  co  m*/

    public static int toUTF8(byte[] outputBuffer, int number) {
        return toUTF8(outputBuffer, (long) number);
    }

    /**
     * Encode the <code>number</code> into UTF8
     * 
     * @param outputBuffer buffer for the encoded number
     * @param number The number to be encoded
     * @return The offset of the <code>outputBuffer</code> the encoded string
     */
    public static int toUTF8(byte[] outputBuffer, long number) {
        long q;
        int r;
        int charPos = outputBuffer.length;
        char sign = 0;

        if (number < 0) {
            sign = '-';
            number = -number;
        }

        // Get 2 digits/iteration using longs until quotient fits into an int
        while (number > Integer.MAX_VALUE) {
            q = number / 100;
            r = (int) (number - ((q << 6) + (q << 5) + (q << 2)));
            number = q;
            outputBuffer[--charPos] = (byte) DigitOnes[r];
            outputBuffer[--charPos] = (byte) DigitTens[r];
        }

        // Get 2 digits/iteration using ints
        int q2;
        int i2 = (int) number;
        while (i2 >= 65536) {
            q2 = i2 / 100;
            r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
            i2 = q2;
            outputBuffer[--charPos] = (byte) DigitOnes[r];
            outputBuffer[--charPos] = (byte) DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        for (;;) {
            q2 = (i2 * 52429) >>> (16 + 3);
            r = i2 - ((q2 << 3) + (q2 << 1));
            outputBuffer[--charPos] = (byte) digits[r];
            i2 = q2;
            if (i2 == 0)
                break;
        }
        if (sign != 0) {
            outputBuffer[--charPos] = (byte) sign;
        }

        return charPos;
    }

    public static int toUTF8(byte[] outputBuffer, String string, char[] workingBuffer) {
        if (string == null) {
            return 0;
        } else {
            int strlen = string.length();
            char[] charr = null;
            if (workingBuffer == null || strlen > workingBuffer.length) {
                charr = new char[strlen];
            } else {
                charr = workingBuffer;
            }
            string.getChars(0, strlen, charr, 0);

            int c;
            int count = 0;
            int utflen = 0;
            for (int i = 0; i < strlen; i++) {
                c = charr[i];
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    utflen++;
                } else if (c > 0x07FF) {
                    utflen += 3;
                } else {
                    utflen += 2;
                }
            }

            byte[] bytearr = null;
            if (outputBuffer == null || utflen > outputBuffer.length) {
                bytearr = new byte[utflen];
            } else {
                bytearr = outputBuffer;
            }

            for (int i = 0; i < strlen; i++) {
                c = charr[i];
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    bytearr[count++] = (byte) c;
                } else if (c > 0x07FF) {
                    bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                    bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
                    bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
                } else {
                    bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
                    bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
                }
            }

            return utflen;
        }
    }

    public static byte[] toUTF8(String string) {
        if (string == null) {
            return null;
        }
        int strlen = string.length();
        char[] charr = new char[strlen];
        string.getChars(0, strlen, charr, 0);

        int c;
        int count = 0;
        int utflen = 0;
        for (int i = 0; i < strlen; i++) {
            c = charr[i];
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }

        byte[] bytearr = new byte[utflen];
        for (int i = 0; i < strlen; i++) {
            c = charr[i];
            if ((c >= 0x0001) && (c <= 0x007F)) {
                bytearr[count++] = (byte) c;
            } else if (c > 0x07FF) {
                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
                bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            } else {
                bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
                bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            }
        }
        return bytearr;
    }
}

Related

  1. toUTF(String inpara)
  2. ToUTF(String s)
  3. toUTF(String str)
  4. toUTF(String... str)
  5. toUtf8(final String string)
  6. toUtf8(String hex)
  7. toUTF8(String oldStr)
  8. toUtf8(String s)