OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / nio / charset / Charsets.java
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 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      */
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      */
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      */
60     public static native byte[] toUtf8Bytes(char[] chars, int offset, int length);
61
62     /**
63      * Decodes the given US-ASCII bytes into the given char[]. Equivalent to but faster than:
64      *
65      * for (int i = 0; i < count; ++i) {
66      *     char ch = (char) (data[start++] & 0xff);
67      *     value[i] = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
68      * }
69      */
70     public static native void asciiBytesToChars(byte[] bytes, int offset, int length, char[] chars);
71
72     /**
73      * Decodes the given ISO-8859-1 bytes into the given char[]. Equivalent to but faster than:
74      *
75      * for (int i = 0; i < count; ++i) {
76      *     value[i] = (char) (data[start++] & 0xff);
77      * }
78      */
79     public static native void isoLatin1BytesToChars(byte[] bytes, int offset, int length, char[] chars);
80
81     private Charsets() {
82     }
83 }