1 /* 2 * Copyright (C) 2010 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 17 package libcore.util; 18 19 import dalvik.annotation.optimization.FastNative; 20 21 /** 22 * Various special-case charset conversions (for performance). 23 * 24 * @hide internal use only 25 */ 26 public final class CharsetUtils { 27 /** 28 * Returns a new byte array containing the bytes corresponding to the characters in the given 29 * string, encoded in US-ASCII. Unrepresentable characters are replaced by (byte) '?'. 30 */ 31 @FastNative toAsciiBytes(String s, int offset, int length)32 public static native byte[] toAsciiBytes(String s, int offset, int length); 33 34 /** 35 * Returns a new byte array containing the bytes corresponding to the characters in the given 36 * string, encoded in ISO-8859-1. Unrepresentable characters are replaced by (byte) '?'. 37 */ 38 @FastNative toIsoLatin1Bytes(String s, int offset, int length)39 public static native byte[] toIsoLatin1Bytes(String s, int offset, int length); 40 41 /** 42 * Returns a new byte array containing the bytes corresponding to the characters in the given 43 * string, encoded in UTF-8. All characters are representable in UTF-8. 44 */ 45 @FastNative toUtf8Bytes(String s, int offset, int length)46 public static native byte[] toUtf8Bytes(String s, int offset, int length); 47 48 /** 49 * Returns a new byte array containing the bytes corresponding to the characters in the given 50 * string, encoded in UTF-16BE. All characters are representable in UTF-16BE. 51 */ toBigEndianUtf16Bytes(String s, int offset, int length)52 public static byte[] toBigEndianUtf16Bytes(String s, int offset, int length) { 53 byte[] result = new byte[length * 2]; 54 int end = offset + length; 55 int resultIndex = 0; 56 for (int i = offset; i < end; ++i) { 57 char ch = s.charAt(i); 58 result[resultIndex++] = (byte) (ch >> 8); 59 result[resultIndex++] = (byte) ch; 60 } 61 return result; 62 } 63 64 /** 65 * Decodes the given US-ASCII bytes into the given char[]. Equivalent to but faster than: 66 * 67 * for (int i = 0; i < count; ++i) { 68 * char ch = (char) (data[start++] & 0xff); 69 * value[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR; 70 * } 71 */ 72 @FastNative asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars)73 public static native void asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars); 74 75 /** 76 * Decodes the given ISO-8859-1 bytes into the given char[]. Equivalent to but faster than: 77 * 78 * for (int i = 0; i < count; ++i) { 79 * value[i] = (char) (data[start++] & 0xff); 80 * } 81 */ 82 @FastNative isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars)83 public static native void isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars); 84 CharsetUtils()85 private CharsetUtils() { 86 } 87 } 88