From 1ed75bde09275c9f2bd1f81830e381ea864ac7f5 Mon Sep 17 00:00:00 2001 From: Deepanshu Gupta Date: Tue, 10 Sep 2013 15:41:21 -0700 Subject: [PATCH] Remove references to non-std Java classes. [DO NOT MERGE] This change adds a substitute for the Charsets class in java namespace. The original cherry pick of the change had dropped this substitute. But the substitute is required for bug b.android.com/59732 The original cherry-pick is 6335590aa8997944cc7fb358a315dbb9b789f87f and the original change is 81f74f4bc96ea4266cebe1b785d095558f540800 The original Change-Id is Iac5b272652e2780c9bb72d19f415d150948ca589 Change-Id: I76c87c74ee088778b37d1a7a7d531e0fde5c758f --- tools/layoutlib/create/README.txt | 3 +- .../tools/layoutlib/create/CreateInfo.java | 3 + .../tools/layoutlib/java/Charsets.java | 133 ++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tools/layoutlib/create/src/com/android/tools/layoutlib/java/Charsets.java diff --git a/tools/layoutlib/create/README.txt b/tools/layoutlib/create/README.txt index 3ced3b646547f..e490ac573bd99 100644 --- a/tools/layoutlib/create/README.txt +++ b/tools/layoutlib/create/README.txt @@ -172,7 +172,8 @@ This is the easiest: we currently inject the following classes: - AutoCloseable is part of Java 7. To enable us to still run on Java 6, a new class is injected. The implementation for the class has been taken from Android's libcore (platform/libcore/luni/src/main/java/java/lang/AutoCloseable.java). -- ModifiedUtf8, IntegralToString and UnsafeByteSequence are not part of the standard JAVA VM. +- Charsets, ModifiedUtf8, IntegralToString and UnsafeByteSequence are not part of the + standard JAVA VM. They are added to the Dalvik VM for performance reasons. An implementation that is very close to the original (which is at platform/libcore/luni/src/main/java/...) is injected. Since these classees were in part of the java package, where we can't inject classes, diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java index 0ded1f5ca0189..195ba95f991fd 100644 --- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java +++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java @@ -19,6 +19,7 @@ package com.android.tools.layoutlib.create; import com.android.tools.layoutlib.annotations.LayoutlibDelegate; import com.android.tools.layoutlib.java.AutoCloseable; import com.android.tools.layoutlib.java.ModifiedUtf8; +import com.android.tools.layoutlib.java.Charsets; import com.android.tools.layoutlib.java.IntegralToString; import com.android.tools.layoutlib.java.UnsafeByteSequence; @@ -114,6 +115,7 @@ public final class CreateInfo implements ICreateInfo { IntegralToString.class, UnsafeByteSequence.class, ModifiedUtf8.class, + Charsets.class, }; /** @@ -222,6 +224,7 @@ public final class CreateInfo implements ICreateInfo { new String[] { "java.lang.AutoCloseable", "com.android.tools.layoutlib.java.AutoCloseable", "java.nio.charset.ModifiedUtf8", "com.android.tools.layoutlib.java.ModifiedUtf8", + "java.nio.charset.Charsets", "com.android.tools.layoutlib.java.Charsets", "java.lang.IntegralToString", "com.android.tools.layoutlib.java.IntegralToString", "java.lang.UnsafeByteSequence", "com.android.tools.layoutlib.java.UnsafeByteSequence", }; diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/java/Charsets.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/java/Charsets.java new file mode 100644 index 0000000000000..f73b06b5ffba1 --- /dev/null +++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/java/Charsets.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed 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.android.tools.layoutlib.java; + +import java.nio.CharBuffer; +import java.nio.charset.Charset; + +/** + * Defines the same class as the java.nio.charset.Charsets which was added in + * Dalvik VM. This hack, provides a replacement for that class which can't be + * loaded in the standard JVM since it's in the java package and standard JVM + * doesn't have it. An implementation of the native methods in the original + * class has been added. + *

+ * Extracted from API level 18, file: + * platform/libcore/luni/src/main/java/java/nio/charset/Charsets + */ +public final class Charsets { + /** + * A cheap and type-safe constant for the ISO-8859-1 Charset. + */ + public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); + + /** + * A cheap and type-safe constant for the US-ASCII Charset. + */ + public static final Charset US_ASCII = Charset.forName("US-ASCII"); + + /** + * A cheap and type-safe constant for the UTF-8 Charset. + */ + public static final Charset UTF_8 = Charset.forName("UTF-8"); + + /** + * Returns a new byte array containing the bytes corresponding to the given characters, + * encoded in US-ASCII. Unrepresentable characters are replaced by (byte) '?'. + */ + public static byte[] toAsciiBytes(char[] chars, int offset, int length) { + CharBuffer cb = CharBuffer.allocate(length); + cb.put(chars, offset, length); + return US_ASCII.encode(cb).array(); + } + + /** + * Returns a new byte array containing the bytes corresponding to the given characters, + * encoded in ISO-8859-1. Unrepresentable characters are replaced by (byte) '?'. + */ + public static byte[] toIsoLatin1Bytes(char[] chars, int offset, int length) { + CharBuffer cb = CharBuffer.allocate(length); + cb.put(chars, offset, length); + return ISO_8859_1.encode(cb).array(); + } + + /** + * Returns a new byte array containing the bytes corresponding to the given characters, + * encoded in UTF-8. All characters are representable in UTF-8. + */ + public static byte[] toUtf8Bytes(char[] chars, int offset, int length) { + CharBuffer cb = CharBuffer.allocate(length); + cb.put(chars, offset, length); + return UTF_8.encode(cb).array(); + } + + /** + * Returns a new byte array containing the bytes corresponding to the given characters, + * encoded in UTF-16BE. All characters are representable in UTF-16BE. + */ + public static byte[] toBigEndianUtf16Bytes(char[] chars, int offset, int length) { + byte[] result = new byte[length * 2]; + int end = offset + length; + int resultIndex = 0; + for (int i = offset; i < end; ++i) { + char ch = chars[i]; + result[resultIndex++] = (byte) (ch >> 8); + result[resultIndex++] = (byte) ch; + } + return result; + } + + /** + * Decodes the given US-ASCII bytes into the given char[]. Equivalent to but faster than: + * + * for (int i = 0; i < count; ++i) { + * char ch = (char) (data[start++] & 0xff); + * value[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR; + * } + */ + public static void asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars) { + if (bytes == null || chars == null) { + return; + } + final char REPLACEMENT_CHAR = (char)0xffd; + int start = offset; + for (int i = 0; i < length; ++i) { + char ch = (char) (bytes[start++] & 0xff); + chars[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR; + } + } + + /** + * Decodes the given ISO-8859-1 bytes into the given char[]. Equivalent to but faster than: + * + * for (int i = 0; i < count; ++i) { + * value[i] = (char) (data[start++] & 0xff); + * } + */ + public static void isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars) { + if (bytes == null || chars == null) { + return; + } + int start = offset; + for (int i = 0; i < length; ++i) { + chars[i] = (char) (bytes[start++] & 0xff); + } + } + + private Charsets() { + } +}