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 * 30 * @hide 31 */ 32 public class CharsetUtils { 33 /** 34 * Attempt to encode the given string as modified UTF-8 into the destination 35 * byte array without making any new allocations. 36 * 37 * @param src string value to be encoded 38 * @param dest destination byte array to encode into 39 * @param destOff offset into destination where encoding should begin 40 * @param destLen length of destination 41 * @return positive value when encoding succeeded, or negative value when 42 * failed; the magnitude of the value is the number of bytes 43 * required to encode the string. 44 */ toModifiedUtf8Bytes(@onNull String src, long dest, int destOff, int destLen)45 public static int toModifiedUtf8Bytes(@NonNull String src, 46 long dest, int destOff, int destLen) { 47 return toModifiedUtf8Bytes(src, src.length(), dest, destOff, destLen); 48 } 49 50 /** 51 * Attempt to encode the given string as modified UTF-8 into the destination 52 * byte array without making any new allocations. 53 * 54 * @param src string value to be encoded 55 * @param srcLen exact length of string to be encoded 56 * @param dest destination byte array to encode into 57 * @param destOff offset into destination where encoding should begin 58 * @param destLen length of destination 59 * @return positive value when encoding succeeded, or negative value when 60 * failed; the magnitude of the value is the number of bytes 61 * required to encode the string. 62 */ 63 @FastNative toModifiedUtf8Bytes(@onNull String src, int srcLen, long dest, int destOff, int destLen)64 private static native int toModifiedUtf8Bytes(@NonNull String src, int srcLen, 65 long dest, int destOff, int destLen); 66 67 /** 68 * Attempt to decode a modified UTF-8 string from the source byte array. 69 * 70 * @param src source byte array to decode from 71 * @param srcOff offset into source where decoding should begin 72 * @param srcLen length of source that should be decoded 73 * @return the successfully decoded string 74 */ 75 @FastNative fromModifiedUtf8Bytes( long src, int srcOff, int srcLen)76 public static native @NonNull String fromModifiedUtf8Bytes( 77 long src, int srcOff, int srcLen); 78 } 79