blob: 178d5dabbd30dfe62908632e39492f1db3822a88 [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"
Andreas Gampee15b9b12018-10-29 12:54:27 -070020#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010021#include "jni/jni_internal.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "mirror/object-inl.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070023#include "mirror/string-alloc-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070024#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070025#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070026#include "nativehelper/scoped_local_ref.h"
27#include "nativehelper/scoped_primitive_array.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_fast_native_object_access-inl.h"
29#include "scoped_thread_state_change-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080030
31namespace art {
32
33static 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());
Mathieu Chartier0795f232016-09-27 18:43:30 -070041 Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(java_data)));
Jeff Hao848f70a2014-01-15 13:49:50 -080042 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();
Vladimir Marko9b81ac32019-05-16 16:47:08 +010050 ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray(soa.Self(),
51 byte_count,
52 byte_array,
53 offset,
54 high,
55 allocator_type);
Jeff Hao848f70a2014-01-15 13:49:50 -080056 return soa.AddLocalReference<jstring>(result);
57}
58
Roland Levillaincc3839c2016-02-29 16:23:48 +000059// The char array passed as `java_data` must not be a null reference.
Jeff Hao848f70a2014-01-15 13:49:50 -080060static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset,
61 jint char_count, jcharArray java_data) {
Roland Levillaincc3839c2016-02-29 16:23:48 +000062 DCHECK(java_data != nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -080063 ScopedFastNativeObjectAccess soa(env);
64 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070065 Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(java_data)));
Jeff Hao848f70a2014-01-15 13:49:50 -080066 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9b81ac32019-05-16 16:47:08 +010067 ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray(soa.Self(),
68 char_count,
69 char_array,
70 offset,
71 allocator_type);
Jeff Hao848f70a2014-01-15 13:49:50 -080072 return soa.AddLocalReference<jstring>(result);
73}
74
75static 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());
Mathieu Chartier0795f232016-09-27 18:43:30 -070082 Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy)));
Jeff Hao848f70a2014-01-15 13:49:50 -080083 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9b81ac32019-05-16 16:47:08 +010084 ObjPtr<mirror::String> result = mirror::String::AllocFromString(soa.Self(),
85 string->GetLength(),
86 string,
87 /*offset=*/ 0,
88 allocator_type);
Jeff Hao848f70a2014-01-15 13:49:50 -080089 return soa.AddLocalReference<jstring>(result);
90}
91
92static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080093 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;"),
Jeff Hao848f70a2014-01-15 13:49:50 -080096};
97
98void register_java_lang_StringFactory(JNIEnv* env) {
99 REGISTER_NATIVE_METHODS("java/lang/StringFactory");
100}
101
102} // namespace art