1 /*
2 * Copyright (C) 2008 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 #include "java_lang_StringFactory.h"
18
19 #include "common_throws.h"
20 #include "jni_internal.h"
21 #include "mirror/object-inl.h"
22 #include "mirror/string.h"
23 #include "scoped_fast_native_object_access-inl.h"
24 #include "scoped_thread_state_change-inl.h"
25 #include "ScopedLocalRef.h"
26 #include "ScopedPrimitiveArray.h"
27
28 namespace art {
29
StringFactory_newStringFromBytes(JNIEnv * env,jclass,jbyteArray java_data,jint high,jint offset,jint byte_count)30 static jstring StringFactory_newStringFromBytes(JNIEnv* env, jclass, jbyteArray java_data,
31 jint high, jint offset, jint byte_count) {
32 ScopedFastNativeObjectAccess soa(env);
33 if (UNLIKELY(java_data == nullptr)) {
34 ThrowNullPointerException("data == null");
35 return nullptr;
36 }
37 StackHandleScope<1> hs(soa.Self());
38 Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(java_data)));
39 int32_t data_size = byte_array->GetLength();
40 if ((offset | byte_count) < 0 || byte_count > data_size - offset) {
41 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
42 "length=%d; regionStart=%d; regionLength=%d", data_size,
43 offset, byte_count);
44 return nullptr;
45 }
46 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
47 ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray<true>(soa.Self(),
48 byte_count,
49 byte_array,
50 offset,
51 high,
52 allocator_type);
53 return soa.AddLocalReference<jstring>(result);
54 }
55
56 // The char array passed as `java_data` must not be a null reference.
StringFactory_newStringFromChars(JNIEnv * env,jclass,jint offset,jint char_count,jcharArray java_data)57 static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset,
58 jint char_count, jcharArray java_data) {
59 DCHECK(java_data != nullptr);
60 ScopedFastNativeObjectAccess soa(env);
61 StackHandleScope<1> hs(soa.Self());
62 Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(java_data)));
63 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
64 ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray<true>(soa.Self(),
65 char_count,
66 char_array,
67 offset,
68 allocator_type);
69 return soa.AddLocalReference<jstring>(result);
70 }
71
StringFactory_newStringFromString(JNIEnv * env,jclass,jstring to_copy)72 static jstring StringFactory_newStringFromString(JNIEnv* env, jclass, jstring to_copy) {
73 ScopedFastNativeObjectAccess soa(env);
74 if (UNLIKELY(to_copy == nullptr)) {
75 ThrowNullPointerException("toCopy == null");
76 return nullptr;
77 }
78 StackHandleScope<1> hs(soa.Self());
79 Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy)));
80 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
81 ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(),
82 string->GetLength(),
83 string,
84 0,
85 allocator_type);
86 return soa.AddLocalReference<jstring>(result);
87 }
88
89 static JNINativeMethod gMethods[] = {
90 FAST_NATIVE_METHOD(StringFactory, newStringFromBytes, "([BIII)Ljava/lang/String;"),
91 FAST_NATIVE_METHOD(StringFactory, newStringFromChars, "(II[C)Ljava/lang/String;"),
92 FAST_NATIVE_METHOD(StringFactory, newStringFromString, "(Ljava/lang/String;)Ljava/lang/String;"),
93 };
94
register_java_lang_StringFactory(JNIEnv * env)95 void register_java_lang_StringFactory(JNIEnv* env) {
96 REGISTER_NATIVE_METHODS("java/lang/StringFactory");
97 }
98
99 } // namespace art
100