103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta/*
203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Copyright (C) 2013 The Android Open Source Project
303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta *
403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * you may not use this file except in compliance with the License.
603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * You may obtain a copy of the License at
703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta *
803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta *
1003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
1103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
1203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * See the License for the specific language governing permissions and
1403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * limitations under the License.
1503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta */
1603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
1703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptapackage com.android.tools.layoutlib.java;
1803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
1903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptaimport java.nio.CharBuffer;
2003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptaimport java.nio.charset.Charset;
2103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
2203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta/**
2303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Defines the same class as the java.nio.charset.Charsets which was added in
2403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Dalvik VM. This hack, provides a replacement for that class which can't be
2503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * loaded in the standard JVM since it's in the java package and standard JVM
2603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * doesn't have it. An implementation of the native methods in the original
2703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * class has been added.
2803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * <p/>
2903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Extracted from API level 18, file:
3003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * platform/libcore/luni/src/main/java/java/nio/charset/Charsets
3103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta */
3203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptapublic final class Charsets {
3303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
3403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * A cheap and type-safe constant for the ISO-8859-1 Charset.
3503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
3603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
3703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
3803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
3903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * A cheap and type-safe constant for the US-ASCII Charset.
4003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
4103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static final Charset US_ASCII = Charset.forName("US-ASCII");
4203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
4303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
4403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * A cheap and type-safe constant for the UTF-8 Charset.
4503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
4603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static final Charset UTF_8 = Charset.forName("UTF-8");
4703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
4803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
4903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * Returns a new byte array containing the bytes corresponding to the given characters,
5003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * encoded in US-ASCII. Unrepresentable characters are replaced by (byte) '?'.
5103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
5203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static byte[] toAsciiBytes(char[] chars, int offset, int length) {
5303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        CharBuffer cb = CharBuffer.allocate(length);
5403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        cb.put(chars, offset, length);
5503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        return US_ASCII.encode(cb).array();
5603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
5703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
5803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
5903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * Returns a new byte array containing the bytes corresponding to the given characters,
6003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * encoded in ISO-8859-1. Unrepresentable characters are replaced by (byte) '?'.
6103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
6203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static byte[] toIsoLatin1Bytes(char[] chars, int offset, int length) {
6303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        CharBuffer cb = CharBuffer.allocate(length);
6403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        cb.put(chars, offset, length);
6503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        return ISO_8859_1.encode(cb).array();
6603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
6703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
6803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
6903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * Returns a new byte array containing the bytes corresponding to the given characters,
7003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * encoded in UTF-8. All characters are representable in UTF-8.
7103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
7203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static byte[] toUtf8Bytes(char[] chars, int offset, int length) {
7303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        CharBuffer cb = CharBuffer.allocate(length);
7403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        cb.put(chars, offset, length);
7503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        return UTF_8.encode(cb).array();
7603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
7703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
7803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
7903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * Returns a new byte array containing the bytes corresponding to the given characters,
8003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * encoded in UTF-16BE. All characters are representable in UTF-16BE.
8103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
8203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static byte[] toBigEndianUtf16Bytes(char[] chars, int offset, int length) {
8303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        byte[] result = new byte[length * 2];
8403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        int end = offset + length;
8503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        int resultIndex = 0;
8603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        for (int i = offset; i < end; ++i) {
8703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            char ch = chars[i];
8803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            result[resultIndex++] = (byte) (ch >> 8);
8903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            result[resultIndex++] = (byte) ch;
9003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        }
9103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        return result;
9203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
9303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
9403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
9503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * Decodes the given US-ASCII bytes into the given char[]. Equivalent to but faster than:
9603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     *
9703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * for (int i = 0; i < count; ++i) {
9803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     *     char ch = (char) (data[start++] & 0xff);
9903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     *     value[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
10003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * }
10103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
10203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static void asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars) {
10303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        if (bytes == null || chars == null) {
10403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            return;
10503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        }
10603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        final char REPLACEMENT_CHAR = (char)0xffd;
10703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        int start = offset;
10803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        for (int i = 0; i < length; ++i) {
10903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            char ch = (char) (bytes[start++] & 0xff);
11003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            chars[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
11103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        }
11203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
11303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
11403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    /**
11503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * Decodes the given ISO-8859-1 bytes into the given char[]. Equivalent to but faster than:
11603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     *
11703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * for (int i = 0; i < count; ++i) {
11803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     *     value[i] = (char) (data[start++] & 0xff);
11903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     * }
12003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta     */
12103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    public static void isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars) {
12203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        if (bytes == null || chars == null) {
12303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            return;
12403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        }
12503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        int start = offset;
12603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        for (int i = 0; i < length; ++i) {
12703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta            chars[i] = (char) (bytes[start++] & 0xff);
12803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta        }
12903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
13003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta
13103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    private Charsets() {
13203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta    }
13303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta}
134