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" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 20 | #include "jni/jni_internal.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 21 | #include "mirror/object-inl.h" |
| 22 | #include "mirror/string.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 23 | #include "native_util.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 24 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 25 | #include "nativehelper/scoped_local_ref.h" |
| 26 | #include "nativehelper/scoped_primitive_array.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 27 | #include "scoped_fast_native_object_access-inl.h" |
| 28 | #include "scoped_thread_state_change-inl.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | static jstring StringFactory_newStringFromBytes(JNIEnv* env, jclass, jbyteArray java_data, |
| 33 | jint high, jint offset, jint byte_count) { |
| 34 | ScopedFastNativeObjectAccess soa(env); |
| 35 | if (UNLIKELY(java_data == nullptr)) { |
| 36 | ThrowNullPointerException("data == null"); |
| 37 | return nullptr; |
| 38 | } |
| 39 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 40 | 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] | 41 | int32_t data_size = byte_array->GetLength(); |
| 42 | if ((offset | byte_count) < 0 || byte_count > data_size - offset) { |
| 43 | soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;", |
| 44 | "length=%d; regionStart=%d; regionLength=%d", data_size, |
| 45 | offset, byte_count); |
| 46 | return nullptr; |
| 47 | } |
| 48 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 49 | ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray<true>(soa.Self(), |
| 50 | byte_count, |
| 51 | byte_array, |
| 52 | offset, |
| 53 | high, |
| 54 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 55 | return soa.AddLocalReference<jstring>(result); |
| 56 | } |
| 57 | |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 58 | // 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] | 59 | static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset, |
| 60 | jint char_count, jcharArray java_data) { |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 61 | DCHECK(java_data != nullptr); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 62 | ScopedFastNativeObjectAccess soa(env); |
| 63 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 64 | 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] | 65 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 66 | ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray<true>(soa.Self(), |
| 67 | char_count, |
| 68 | char_array, |
| 69 | offset, |
| 70 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 71 | return soa.AddLocalReference<jstring>(result); |
| 72 | } |
| 73 | |
| 74 | static jstring StringFactory_newStringFromString(JNIEnv* env, jclass, jstring to_copy) { |
| 75 | ScopedFastNativeObjectAccess soa(env); |
| 76 | if (UNLIKELY(to_copy == nullptr)) { |
| 77 | ThrowNullPointerException("toCopy == null"); |
| 78 | return nullptr; |
| 79 | } |
| 80 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 81 | Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy))); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 82 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 83 | ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(), |
| 84 | string->GetLength(), |
| 85 | string, |
| 86 | 0, |
| 87 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 88 | return soa.AddLocalReference<jstring>(result); |
| 89 | } |
| 90 | |
| 91 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 92 | FAST_NATIVE_METHOD(StringFactory, newStringFromBytes, "([BIII)Ljava/lang/String;"), |
| 93 | FAST_NATIVE_METHOD(StringFactory, newStringFromChars, "(II[C)Ljava/lang/String;"), |
| 94 | FAST_NATIVE_METHOD(StringFactory, newStringFromString, "(Ljava/lang/String;)Ljava/lang/String;"), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | void register_java_lang_StringFactory(JNIEnv* env) { |
| 98 | REGISTER_NATIVE_METHODS("java/lang/StringFactory"); |
| 99 | } |
| 100 | |
| 101 | } // namespace art |