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 "scoped_fast_native_object_access.h"
24#include "scoped_thread_state_change.h"
25#include "ScopedLocalRef.h"
26#include "ScopedPrimitiveArray.h"
27
28namespace art {
29
30static 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());
38  Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray*>(java_data)));
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();
47  mirror::String* result = mirror::String::AllocFromByteArray<true>(soa.Self(), byte_count,
48                                                                    byte_array, offset, high,
49                                                                    allocator_type);
50  return soa.AddLocalReference<jstring>(result);
51}
52
53// The char array passed as `java_data` must not be a null reference.
54static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset,
55                                                jint char_count, jcharArray java_data) {
56  DCHECK(java_data != nullptr);
57  ScopedFastNativeObjectAccess soa(env);
58  StackHandleScope<1> hs(soa.Self());
59  Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray*>(java_data)));
60  gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
61  mirror::String* result = mirror::String::AllocFromCharArray<true>(soa.Self(), char_count,
62                                                                    char_array, offset,
63                                                                    allocator_type);
64  return soa.AddLocalReference<jstring>(result);
65}
66
67static jstring StringFactory_newStringFromString(JNIEnv* env, jclass, jstring to_copy) {
68  ScopedFastNativeObjectAccess soa(env);
69  if (UNLIKELY(to_copy == nullptr)) {
70    ThrowNullPointerException("toCopy == null");
71    return nullptr;
72  }
73  StackHandleScope<1> hs(soa.Self());
74  Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String*>(to_copy)));
75  gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
76  mirror::String* result = mirror::String::AllocFromString<true>(soa.Self(), string->GetLength(),
77                                                                 string, 0, allocator_type);
78  return soa.AddLocalReference<jstring>(result);
79}
80
81static JNINativeMethod gMethods[] = {
82  NATIVE_METHOD(StringFactory, newStringFromBytes, "!([BIII)Ljava/lang/String;"),
83  NATIVE_METHOD(StringFactory, newStringFromChars, "!(II[C)Ljava/lang/String;"),
84  NATIVE_METHOD(StringFactory, newStringFromString, "!(Ljava/lang/String;)Ljava/lang/String;"),
85};
86
87void register_java_lang_StringFactory(JNIEnv* env) {
88  REGISTER_NATIVE_METHODS("java/lang/StringFactory");
89}
90
91}  // namespace art
92