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" |
| 20 | #include "jni_internal.h" |
| 21 | #include "mirror/object-inl.h" |
| 22 | #include "mirror/string.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_fast_native_object_access-inl.h" |
| 24 | #include "scoped_thread_state_change-inl.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 25 | #include "ScopedLocalRef.h" |
| 26 | #include "ScopedPrimitiveArray.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | static 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 Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | 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] | 39 | 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 Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 47 | ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray<true>(soa.Self(), |
| 48 | byte_count, |
| 49 | byte_array, |
| 50 | offset, |
| 51 | high, |
| 52 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 53 | return soa.AddLocalReference<jstring>(result); |
| 54 | } |
| 55 | |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 56 | // 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] | 57 | static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset, |
| 58 | jint char_count, jcharArray java_data) { |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 59 | DCHECK(java_data != nullptr); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 60 | ScopedFastNativeObjectAccess soa(env); |
| 61 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 62 | 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] | 63 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 64 | ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray<true>(soa.Self(), |
| 65 | char_count, |
| 66 | char_array, |
| 67 | offset, |
| 68 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 69 | return soa.AddLocalReference<jstring>(result); |
| 70 | } |
| 71 | |
| 72 | static 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 Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 79 | Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy))); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 80 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 81 | ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(), |
| 82 | string->GetLength(), |
| 83 | string, |
| 84 | 0, |
| 85 | allocator_type); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 86 | return soa.AddLocalReference<jstring>(result); |
| 87 | } |
| 88 | |
| 89 | static JNINativeMethod gMethods[] = { |
| 90 | NATIVE_METHOD(StringFactory, newStringFromBytes, "!([BIII)Ljava/lang/String;"), |
| 91 | NATIVE_METHOD(StringFactory, newStringFromChars, "!(II[C)Ljava/lang/String;"), |
| 92 | NATIVE_METHOD(StringFactory, newStringFromString, "!(Ljava/lang/String;)Ljava/lang/String;"), |
| 93 | }; |
| 94 | |
| 95 | void register_java_lang_StringFactory(JNIEnv* env) { |
| 96 | REGISTER_NATIVE_METHODS("java/lang/StringFactory"); |
| 97 | } |
| 98 | |
| 99 | } // namespace art |