Debian Codegraph @

core/java/android/util/CharsetUtils.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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 android.util;
18
19 import android.annotation.NonNull;
20
21 import dalvik.annotation.optimization.FastNative;
22
23 /**
24 * Specializations of {@code libcore.util.CharsetUtils} which enable efficient
25 * in-place encoding without making any new allocations.
26 * <p>
27 * These methods purposefully accept only non-movable byte array addresses to
28 * avoid extra JNI overhead.
29 * <p>
30 * Callers are cautioned that there is a long-standing ART bug that emits
31 * non-standard 4-byte sequences, as described by {@code kUtfUse4ByteSequence}
32 * in {@code art/runtime/jni/jni_internal.cc}. If precise modified UTF-8
33 * encoding is required, use {@link com.android.internal.util.ModifiedUtf8}
34 * instead.
35 *
36 * @hide
37 */
38 public class CharsetUtils {
39 /**
40 * Attempt to encode the given string as modified UTF-8 into the destination
41 * byte array without making any new allocations.
42 * <p>
43 * Callers are cautioned that there is a long-standing ART bug that emits
44 * non-standard 4-byte sequences, as described by
45 * {@code kUtfUse4ByteSequence} in {@code art/runtime/jni/jni_internal.cc}.
46 * If precise modified UTF-8 encoding is required, use
47 * {@link com.android.internal.util.ModifiedUtf8} instead.
48 *
49 * @param src string value to be encoded
50 * @param dest destination byte array to encode into
51 * @param destOff offset into destination where encoding should begin
52 * @param destLen length of destination
53 * @return positive value when encoding succeeded, or negative value when
54 * failed; the magnitude of the value is the number of bytes
55 * required to encode the string.
56 */
57 public static int toModifiedUtf8Bytes(@NonNull String src,
58 long dest, int destOff, int destLen) {
59 return toModifiedUtf8Bytes(src, src.length(), dest, destOff, destLen);
60 }
61
62 /**
63 * Attempt to encode the given string as modified UTF-8 into the destination
64 * byte array without making any new allocations.
65 * <p>
66 * Callers are cautioned that there is a long-standing ART bug that emits
67 * non-standard 4-byte sequences, as described by
68 * {@code kUtfUse4ByteSequence} in {@code art/runtime/jni/jni_internal.cc}.
69 * If precise modified UTF-8 encoding is required, use
70 * {@link com.android.internal.util.ModifiedUtf8} instead.
71 *
72 * @param src string value to be encoded
73 * @param srcLen exact length of string to be encoded
74 * @param dest destination byte array to encode into
75 * @param destOff offset into destination where encoding should begin
76 * @param destLen length of destination
77 * @return positive value when encoding succeeded, or negative value when
78 * failed; the magnitude of the value is the number of bytes
79 * required to encode the string.
80 */
81 @FastNative
82 private static native int toModifiedUtf8Bytes(@NonNull String src, int srcLen,
83 long dest, int destOff, int destLen);
84
85 /**
86 * Attempt to decode a modified UTF-8 string from the source byte array.
87 * <p>
88 * Callers are cautioned that there is a long-standing ART bug that emits
89 * non-standard 4-byte sequences, as described by
90 * {@code kUtfUse4ByteSequence} in {@code art/runtime/jni/jni_internal.cc}.
91 * If precise modified UTF-8 encoding is required, use
92 * {@link com.android.internal.util.ModifiedUtf8} instead.
93 *
94 * @param src source byte array to decode from
95 * @param srcOff offset into source where decoding should begin
96 * @param srcLen length of source that should be decoded
97 * @return the successfully decoded string
98 */
99 @FastNative
100 public static native @NonNull String fromModifiedUtf8Bytes(
101 long src, int srcOff, int srcLen);
102 }