Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 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" |
Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 20 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 21 | #include "jni/jni_internal.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 22 | #include "mirror/object-inl.h" |
Andreas Gampe | fd63bbf | 2018-10-29 12:55:35 -0700 | [diff] [blame] | 23 | #include "mirror/string-alloc-inl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 24 | #include "native_util.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 25 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 26 | #include "nativehelper/scoped_local_ref.h" |
| 27 | #include "nativehelper/scoped_primitive_array.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 28 | #include "scoped_fast_native_object_access-inl.h" |
| 29 | #include "scoped_thread_state_change-inl.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| 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()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 41 | Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(java_data))); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 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(); |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 50 | ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray(soa.Self(), |
| 51 | byte_count, |
| 52 | byte_array, |
| 53 | offset, |
| 54 | high, |
| 55 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 56 | return soa.AddLocalReference<jstring>(result); |
| 57 | } |
| 58 | |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 59 | // The char array passed as `java_data` must not be a null reference. |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 60 | static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset, |
| 61 | jint char_count, jcharArray java_data) { |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 62 | DCHECK(java_data != nullptr); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 63 | ScopedFastNativeObjectAccess soa(env); |
| 64 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 65 | Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(java_data))); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 66 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 67 | ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray(soa.Self(), |
| 68 | char_count, |
| 69 | char_array, |
| 70 | offset, |
| 71 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 72 | return soa.AddLocalReference<jstring>(result); |
| 73 | } |
| 74 | |
| 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()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 82 | Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy))); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 83 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 84 | ObjPtr<mirror::String> result = mirror::String::AllocFromString(soa.Self(), |
| 85 | string->GetLength(), |
| 86 | string, |
| 87 | /*offset=*/ 0, |
| 88 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 89 | return soa.AddLocalReference<jstring>(result); |
| 90 | } |
| 91 | |
| 92 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 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;"), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | void register_java_lang_StringFactory(JNIEnv* env) { |
| 99 | REGISTER_NATIVE_METHODS("java/lang/StringFactory"); |
| 100 | } |
| 101 | |
| 102 | } // namespace art |