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