This method assumes valid UTF8 input. - Java java.lang

Java examples for java.lang:String UTF

Description

This method assumes valid UTF8 input.

Demo Code

/*/* w  ww  .  j a  v  a 2s  .  co m*/
 * 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 final int[] utf8CodeLength;
    /**
     * <p>This method assumes valid UTF8 input. This method 
     * <strong>does not perform</strong> full UTF8 validation, it will check only the 
     * first byte of each codepoint (for multi-byte sequences any bytes after 
     * the head are skipped). It is the responsibility of the caller to make sure
     * that the destination array is large enough.
     * 
     * @throws IllegalArgumentException If invalid codepoint header byte occurs or the 
     *    content is prematurely truncated.
     */
    public static int UTF8toUTF32(final BytesRef utf8, final int[] ints) {
        // TODO: ints cannot be null, should be an assert
        int utf32Count = 0;
        int utf8Upto = utf8.offset;
        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 IllegalArgumentException("invalid utf8");
            }

            // TODO: this may read past utf8's limit.
            final int limit = utf8Upto + numBytes - 1;
            while (utf8Upto < limit) {
                v = v << 6 | bytes[utf8Upto++] & 63;
            }
            ints[utf32Count++] = v;
        }

        return utf32Count;
    }
}

Related Tutorials