2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "String"
20 #include "JniConstants.h"
21 #include "ScopedPrimitiveArray.h"
23 #include "unicode/utf16.h"
28 * Approximates java.lang.UnsafeByteSequence so we don't have to pay the cost of calling back into
29 * Java when converting a char[] to a UTF-8 byte[]. This lets us have UTF-8 conversions slightly
30 * faster than ICU for large char[]s without paying for the NIO overhead with small char[]s.
32 * We could avoid this by keeping the UTF-8 bytes on the native heap until we're done and only
33 * creating a byte[] on the Java heap when we know how big it needs to be, but one shouldn't lie
34 * to the garbage collector (nor hide potentially large allocations from it).
36 * Because a call to append might require an allocation, it might fail. Callers should always
37 * check the return value of append.
39 class NativeUnsafeByteSequence {
41 NativeUnsafeByteSequence(JNIEnv* env)
42 : mEnv(env), mJavaArray(NULL), mRawArray(NULL), mSize(-1), mOffset(0)
46 ~NativeUnsafeByteSequence() {
47 // Release our pointer to the raw array, copying changes back to the Java heap.
48 if (mRawArray != NULL) {
49 mEnv->ReleaseByteArrayElements(mJavaArray, mRawArray, 0);
53 bool append(jbyte b) {
54 if (mOffset == mSize && !resize(mSize * 2)) {
57 mRawArray[mOffset++] = b;
61 bool resize(int newSize) {
62 if (newSize == mSize) {
66 // Allocate a new array.
67 jbyteArray newJavaArray = mEnv->NewByteArray(newSize);
68 if (newJavaArray == NULL) {
71 jbyte* newRawArray = mEnv->GetByteArrayElements(newJavaArray, NULL);
72 if (newRawArray == NULL) {
76 // Copy data out of the old array and then let go of it.
77 // Note that we may be trimming the array.
78 if (mRawArray != NULL) {
79 memcpy(newRawArray, mRawArray, mOffset);
80 mEnv->ReleaseByteArrayElements(mJavaArray, mRawArray, JNI_ABORT);
83 // Point ourselves at the new array.
84 mJavaArray = newJavaArray;
85 mRawArray = newRawArray;
90 jbyteArray toByteArray() {
91 // Trim any unused space, if necessary.
92 bool okay = resize(mOffset);
93 return okay ? mJavaArray : NULL;
98 jbyteArray mJavaArray;
103 // Disallow copy and assignment.
104 NativeUnsafeByteSequence(const NativeUnsafeByteSequence&);
105 void operator=(const NativeUnsafeByteSequence&);
108 static void Charsets_asciiBytesToChars(JNIEnv* env, jclass, jbyteArray javaBytes, jint offset, jint length, jcharArray javaChars) {
109 ScopedByteArrayRO bytes(env, javaBytes);
110 if (bytes.get() == NULL) {
113 ScopedCharArrayRW chars(env, javaChars);
114 if (chars.get() == NULL) {
118 const jbyte* src = &bytes[offset];
119 jchar* dst = &chars[0];
120 static const jchar REPLACEMENT_CHAR = 0xfffd;
121 for (int i = length - 1; i >= 0; --i) {
122 jchar ch = static_cast<jchar>(*src++ & 0xff);
123 *dst++ = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
127 static void Charsets_isoLatin1BytesToChars(JNIEnv* env, jclass, jbyteArray javaBytes, jint offset, jint length, jcharArray javaChars) {
128 ScopedByteArrayRO bytes(env, javaBytes);
129 if (bytes.get() == NULL) {
132 ScopedCharArrayRW chars(env, javaChars);
133 if (chars.get() == NULL) {
137 const jbyte* src = &bytes[offset];
138 jchar* dst = &chars[0];
139 for (int i = length - 1; i >= 0; --i) {
140 *dst++ = static_cast<jchar>(*src++ & 0xff);
145 * Translates the given characters to US-ASCII or ISO-8859-1 bytes, using the fact that
146 * Unicode code points between U+0000 and U+007f inclusive are identical to US-ASCII, while
147 * U+0000 to U+00ff inclusive are identical to ISO-8859-1.
149 static jbyteArray charsToBytes(JNIEnv* env, jcharArray javaChars, jint offset, jint length, jchar maxValidChar) {
150 ScopedCharArrayRO chars(env, javaChars);
151 if (chars.get() == NULL) {
155 jbyteArray javaBytes = env->NewByteArray(length);
156 ScopedByteArrayRW bytes(env, javaBytes);
157 if (bytes.get() == NULL) {
161 const jchar* src = &chars[offset];
162 jbyte* dst = &bytes[0];
163 for (int i = length - 1; i >= 0; --i) {
165 if (ch > maxValidChar) {
168 *dst++ = static_cast<jbyte>(ch);
174 static jbyteArray Charsets_toAsciiBytes(JNIEnv* env, jclass, jcharArray javaChars, jint offset, jint length) {
175 return charsToBytes(env, javaChars, offset, length, 0x7f);
178 static jbyteArray Charsets_toIsoLatin1Bytes(JNIEnv* env, jclass, jcharArray javaChars, jint offset, jint length) {
179 return charsToBytes(env, javaChars, offset, length, 0xff);
182 static jbyteArray Charsets_toUtf8Bytes(JNIEnv* env, jclass, jcharArray javaChars, jint offset, jint length) {
183 ScopedCharArrayRO chars(env, javaChars);
184 if (chars.get() == NULL) {
188 NativeUnsafeByteSequence out(env);
189 if (!out.resize(length)) {
193 const int end = offset + length;
194 for (int i = offset; i < end; ++i) {
198 if (!out.append(ch)) {
201 } else if (ch < 0x800) {
203 if (!out.append((ch >> 6) | 0xc0) || !out.append((ch & 0x3f) | 0x80)) {
206 } else if (U16_IS_SURROGATE(ch)) {
207 // A supplementary character.
208 jchar high = (jchar) ch;
209 jchar low = (i + 1 != end) ? chars[i + 1] : 0;
210 if (!U16_IS_SURROGATE_LEAD(high) || !U16_IS_SURROGATE_TRAIL(low)) {
211 if (!out.append('?')) {
216 // Now we know we have a *valid* surrogate pair, we can consume the low surrogate.
218 ch = U16_GET_SUPPLEMENTARY(high, low);
220 jbyte b1 = (ch >> 18) | 0xf0;
221 jbyte b2 = ((ch >> 12) & 0x3f) | 0x80;
222 jbyte b3 = ((ch >> 6) & 0x3f) | 0x80;
223 jbyte b4 = (ch & 0x3f) | 0x80;
224 if (!out.append(b1) || !out.append(b2) || !out.append(b3) || !out.append(b4)) {
229 jbyte b1 = (ch >> 12) | 0xe0;
230 jbyte b2 = ((ch >> 6) & 0x3f) | 0x80;
231 jbyte b3 = (ch & 0x3f) | 0x80;
232 if (!out.append(b1) || !out.append(b2) || !out.append(b3)) {
237 return out.toByteArray();
240 static JNINativeMethod gMethods[] = {
241 NATIVE_METHOD(Charsets, asciiBytesToChars, "([BII[C)V"),
242 NATIVE_METHOD(Charsets, isoLatin1BytesToChars, "([BII[C)V"),
243 NATIVE_METHOD(Charsets, toAsciiBytes, "([CII)[B"),
244 NATIVE_METHOD(Charsets, toIsoLatin1Bytes, "([CII)[B"),
245 NATIVE_METHOD(Charsets, toUtf8Bytes, "([CII)[B"),
247 int register_java_nio_charset_Charsets(JNIEnv* env) {
248 return jniRegisterNativeMethods(env, "java/nio/charset/Charsets", gMethods, NELEM(gMethods));