Calculates the number of UTF8 bytes necessary to write a UTF16 string. - Java java.lang

Java examples for java.lang:String UTF

Description

Calculates the number of UTF8 bytes necessary to write a UTF16 string.

Demo Code

/*/*www .  j a  va 2s .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.
 */
//package com.java2s;

public class Main {
    /**
     * Calculates the number of UTF8 bytes necessary to write a UTF16 string.
     *
     * @return the number of bytes written
     */
    public static int calcUTF16toUTF8Length(final CharSequence s,
            final int offset, final int len) {
        final int end = offset + len;

        int res = 0;
        for (int i = offset; i < end; i++) {
            final int code = (int) s.charAt(i);

            if (code < 0x80)
                res++;
            else if (code < 0x800) {
                res += 2;
            } else if (code < 0xD800 || code > 0xDFFF) {
                res += 3;
            } else {
                // surrogate pair
                // confirm valid high surrogate
                if (code < 0xDC00 && (i < end - 1)) {
                    int utf32 = (int) s.charAt(i + 1);
                    // confirm valid low surrogate and write pair
                    if (utf32 >= 0xDC00 && utf32 <= 0xDFFF) {
                        i++;
                        res += 4;
                        continue;
                    }
                }
                res += 3;
            }
        }

        return res;
    }
}

Related Tutorials