Home | History | Annotate | Download | only in native
      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"
     23 #include "native_util.h"
     24 #include "nativehelper/jni_macros.h"
     25 #include "nativehelper/scoped_local_ref.h"
     26 #include "nativehelper/scoped_primitive_array.h"
     27 #include "scoped_fast_native_object_access-inl.h"
     28 #include "scoped_thread_state_change-inl.h"
     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());
     40   Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(java_data)));
     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();
     49   ObjPtr<mirror::String> result = mirror::String::AllocFromByteArray<true>(soa.Self(),
     50                                                                            byte_count,
     51                                                                            byte_array,
     52                                                                            offset,
     53                                                                            high,
     54                                                                            allocator_type);
     55   return soa.AddLocalReference<jstring>(result);
     56 }
     57 
     58 // The char array passed as `java_data` must not be a null reference.
     59 static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset,
     60                                                 jint char_count, jcharArray java_data) {
     61   DCHECK(java_data != nullptr);
     62   ScopedFastNativeObjectAccess soa(env);
     63   StackHandleScope<1> hs(soa.Self());
     64   Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(java_data)));
     65   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
     66   ObjPtr<mirror::String> result = mirror::String::AllocFromCharArray<true>(soa.Self(),
     67                                                                            char_count,
     68                                                                            char_array,
     69                                                                            offset,
     70                                                                            allocator_type);
     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());
     81   Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(to_copy)));
     82   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
     83   ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(),
     84                                                                         string->GetLength(),
     85                                                                         string,
     86                                                                         0,
     87                                                                         allocator_type);
     88   return soa.AddLocalReference<jstring>(result);
     89 }
     90 
     91 static JNINativeMethod gMethods[] = {
     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;"),
     95 };
     96 
     97 void register_java_lang_StringFactory(JNIEnv* env) {
     98   REGISTER_NATIVE_METHODS("java/lang/StringFactory");
     99 }
    100 
    101 }  // namespace art
    102