1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tools.layoutlib.java;
18
19import java.nio.CharBuffer;
20import java.nio.charset.Charset;
21
22/**
23 * Defines the same class as the java.nio.charset.Charsets which was added in
24 * Dalvik VM. This hack, provides a replacement for that class which can't be
25 * loaded in the standard JVM since it's in the java package and standard JVM
26 * doesn't have it. An implementation of the native methods in the original
27 * class has been added.
28 * <p/>
29 * Extracted from API level 18, file:
30 * platform/libcore/luni/src/main/java/java/nio/charset/Charsets
31 */
32public final class Charsets {
33    /**
34     * A cheap and type-safe constant for the ISO-8859-1 Charset.
35     */
36    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
37
38    /**
39     * A cheap and type-safe constant for the US-ASCII Charset.
40     */
41    public static final Charset US_ASCII = Charset.forName("US-ASCII");
42
43    /**
44     * A cheap and type-safe constant for the UTF-8 Charset.
45     */
46    public static final Charset UTF_8 = Charset.forName("UTF-8");
47
48    /**
49     * Returns a new byte array containing the bytes corresponding to the given characters,
50     * encoded in US-ASCII. Unrepresentable characters are replaced by (byte) '?'.
51     */
52    public static byte[] toAsciiBytes(char[] chars, int offset, int length) {
53        CharBuffer cb = CharBuffer.allocate(length);
54        cb.put(chars, offset, length);
55        return US_ASCII.encode(cb).array();
56    }
57
58    /**
59     * Returns a new byte array containing the bytes corresponding to the given characters,
60     * encoded in ISO-8859-1. Unrepresentable characters are replaced by (byte) '?'.
61     */
62    public static byte[] toIsoLatin1Bytes(char[] chars, int offset, int length) {
63        CharBuffer cb = CharBuffer.allocate(length);
64        cb.put(chars, offset, length);
65        return ISO_8859_1.encode(cb).array();
66    }
67
68    /**
69     * Returns a new byte array containing the bytes corresponding to the given characters,
70     * encoded in UTF-8. All characters are representable in UTF-8.
71     */
72    public static byte[] toUtf8Bytes(char[] chars, int offset, int length) {
73        CharBuffer cb = CharBuffer.allocate(length);
74        cb.put(chars, offset, length);
75        return UTF_8.encode(cb).array();
76    }
77
78    /**
79     * Returns a new byte array containing the bytes corresponding to the given characters,
80     * encoded in UTF-16BE. All characters are representable in UTF-16BE.
81     */
82    public static byte[] toBigEndianUtf16Bytes(char[] chars, int offset, int length) {
83        byte[] result = new byte[length * 2];
84        int end = offset + length;
85        int resultIndex = 0;
86        for (int i = offset; i < end; ++i) {
87            char ch = chars[i];
88            result[resultIndex++] = (byte) (ch >> 8);
89            result[resultIndex++] = (byte) ch;
90        }
91        return result;
92    }
93
94    /**
95     * Decodes the given US-ASCII bytes into the given char[]. Equivalent to but faster than:
96     *
97     * for (int i = 0; i < count; ++i) {
98     *     char ch = (char) (data[start++] & 0xff);
99     *     value[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
100     * }
101     */
102    public static void asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars) {
103        if (bytes == null || chars == null) {
104            return;
105        }
106        final char REPLACEMENT_CHAR = (char)0xffd;
107        int start = offset;
108        for (int i = 0; i < length; ++i) {
109            char ch = (char) (bytes[start++] & 0xff);
110            chars[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
111        }
112    }
113
114    /**
115     * Decodes the given ISO-8859-1 bytes into the given char[]. Equivalent to but faster than:
116     *
117     * for (int i = 0; i < count; ++i) {
118     *     value[i] = (char) (data[start++] & 0xff);
119     * }
120     */
121    public static void isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars) {
122        if (bytes == null || chars == null) {
123            return;
124        }
125        int start = offset;
126        for (int i = 0; i < length; ++i) {
127            chars[i] = (char) (bytes[start++] & 0xff);
128        }
129    }
130
131    private Charsets() {
132    }
133}
134