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 java.nio.charset; 18 19 /** 20 * Provides convenient access to the most important built-in charsets. Saves a hash lookup and 21 * unnecessary handling of UnsupportedEncodingException at call sites, compared to using the 22 * charset's name. 23 * 24 * Also various special-case charset conversions (for performance). 25 * 26 * @hide internal use only 27 */ 28 public final class Charsets { 29 /** 30 * A cheap and type-safe constant for the ISO-8859-1 Charset. 31 */ 32 public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); 33 34 /** 35 * A cheap and type-safe constant for the US-ASCII Charset. 36 */ 37 public static final Charset US_ASCII = Charset.forName("US-ASCII"); 38 39 /** 40 * A cheap and type-safe constant for the UTF-8 Charset. 41 */ 42 public static final Charset UTF_8 = Charset.forName("UTF-8"); 43 44 /** 45 * Returns a new byte array containing the bytes corresponding to the given characters, 46 * encoded in US-ASCII. Unrepresentable characters are replaced by (byte) '?'. 47 */ toAsciiBytes(char[] chars, int offset, int length)48 public static native byte[] toAsciiBytes(char[] chars, int offset, int length); 49 50 /** 51 * Returns a new byte array containing the bytes corresponding to the given characters, 52 * encoded in ISO-8859-1. Unrepresentable characters are replaced by (byte) '?'. 53 */ toIsoLatin1Bytes(char[] chars, int offset, int length)54 public static native byte[] toIsoLatin1Bytes(char[] chars, int offset, int length); 55 56 /** 57 * Returns a new byte array containing the bytes corresponding to the given characters, 58 * encoded in UTF-8. All characters are representable in UTF-8. 59 */ toUtf8Bytes(char[] chars, int offset, int length)60 public static native byte[] toUtf8Bytes(char[] chars, int offset, int length); 61 62 /** 63 * Returns a new byte array containing the bytes corresponding to the given characters, 64 * encoded in UTF-16BE. All characters are representable in UTF-16BE. 65 */ toBigEndianUtf16Bytes(char[] chars, int offset, int length)66 public static byte[] toBigEndianUtf16Bytes(char[] chars, int offset, int length) { 67 byte[] result = new byte[length * 2]; 68 int end = offset + length; 69 int resultIndex = 0; 70 for (int i = offset; i < end; ++i) { 71 char ch = chars[i]; 72 result[resultIndex++] = (byte) (ch >> 8); 73 result[resultIndex++] = (byte) ch; 74 } 75 return result; 76 } 77 78 /** 79 * Decodes the given US-ASCII bytes into the given char[]. Equivalent to but faster than: 80 * 81 * for (int i = 0; i < count; ++i) { 82 * char ch = (char) (data[start++] & 0xff); 83 * value[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR; 84 * } 85 */ asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars)86 public static native void asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars); 87 88 /** 89 * Decodes the given ISO-8859-1 bytes into the given char[]. Equivalent to but faster than: 90 * 91 * for (int i = 0; i < count; ++i) { 92 * value[i] = (char) (data[start++] & 0xff); 93 * } 94 */ isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars)95 public static native void isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars); 96 Charsets()97 private Charsets() { 98 } 99 } 100