blob: ec3c7c2fdfff0dfff328d99e8ebdd28bda5288be [file] [log] [blame]
Jeff Hao848f70a2014-01-15 13:49:50 -08001/*
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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070023#include "scoped_fast_native_object_access-inl.h"
24#include "scoped_thread_state_change-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080025#include "ScopedLocalRef.h"
26#include "ScopedPrimitiveArray.h"
27
28namespace art {
29
30static 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());
Mathieu Chartier0795f232016-09-27 18:43:30 -070038 Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(java_data)));
Jeff Hao848f70a2014-01-15 13:49:50 -080039 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();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070047 ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray<true>(soa.Self(),
48 byte_count,
49 byte_array,
50 offset,
51 high,
52 allocator_type);
Jeff Hao848f70a2014-01-15 13:49:50 -080053 return soa.AddLocalReference<jstring>(result);
54}
55
Roland Levillaincc3839c2016-02-29 16:23:48 +000056// The char array passed as `java_data` must not be a null reference.
Jeff Hao848f70a2014-01-15 13:49:50 -080057static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset,
58 jint char_count, jcharArray java_data) {
Roland Levillaincc3839c2016-02-29 16:23:48 +000059 DCHECK(java_data != nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -080060 ScopedFastNativeObjectAccess soa(env);
61 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070062 Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(java_data)));
Jeff Hao848f70a2014-01-15 13:49:50 -080063 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070064 ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray<true>(soa.Self(),
65 char_count,
66 char_array,
67 offset,
68 allocator_type);
Jeff Hao848f70a2014-01-15 13:49:50 -080069 return soa.AddLocalReference<jstring>(result);
70}
71
72static 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());
Mathieu Chartier0795f232016-09-27 18:43:30 -070079 Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy)));
Jeff Hao848f70a2014-01-15 13:49:50 -080080 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070081 ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(),
82 string->GetLength(),
83 string,
84 0,
85 allocator_type);
Jeff Hao848f70a2014-01-15 13:49:50 -080086 return soa.AddLocalReference<jstring>(result);
87}
88
89static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080090 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;"),
Jeff Hao848f70a2014-01-15 13:49:50 -080093};
94
95void register_java_lang_StringFactory(JNIEnv* env) {
96 REGISTER_NATIVE_METHODS("java/lang/StringFactory");
97}
98
99} // namespace art