UTF8 to UTF32 - Java java.lang

Java examples for java.lang:String UTF

Description

UTF8 to UTF32

Demo Code

/**//ww w  .ja  v  a2 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{
    static byte[] utf8CodeLength = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2,
            2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
            2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
            4, 4, 4, 4, 4, 4, 4, 4 // , 5, 5, 5, 5, 6, 6, 0, 0
    };
    public static void UTF8toUTF32(final BytesRef utf8, final IntsRef utf32) {
        // pre-alloc for worst case
        if (utf32.ints == null || utf32.ints.length < utf8.length) {
            utf32.ints = new int[utf8.length];
        }
        int utf32Count = 0;
        int utf8Upto = utf8.offset;
        final int[] ints = utf32.ints;
        final byte[] bytes = utf8.bytes;
        final int utf8Limit = utf8.offset + utf8.length;
        while (utf8Upto < utf8Limit) {
            final int numBytes = utf8CodeLength[bytes[utf8Upto] & 0xFF];
            int v = 0;
            switch (numBytes) {
            case 1:
                ints[utf32Count++] = bytes[utf8Upto++];
                continue;
            case 2:
                // 5 useful bits
                v = bytes[utf8Upto++] & 31;
                break;
            case 3:
                // 4 useful bits
                v = bytes[utf8Upto++] & 15;
                break;
            case 4:
                // 3 useful bits
                v = bytes[utf8Upto++] & 7;
                break;
            default:
                throw new IllegalStateException("invalid utf8");
            }

            final int limit = utf8Upto + numBytes - 1;

            while (utf8Upto < limit) {
                v = v << 6 | bytes[utf8Upto++] & 63;
            }
            ints[utf32Count++] = v;
        }

        utf32.offset = 0;
        utf32.length = utf32Count;
    }
}

Related Tutorials