blob: 34d6a37ab2b346beceb2774426679616baf19394 [file] [log] [blame]
Jeff Hao848f70a2014-01-15 13:49:50 -08001/*
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
53static jstring StringFactory_newStringFromChars(JNIEnv* env, jclass, jint offset,
54 jint char_count, jcharArray java_data) {
55 ScopedFastNativeObjectAccess soa(env);
56 StackHandleScope<1> hs(soa.Self());
57 Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray*>(java_data)));
58 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
59 mirror::String* result = mirror::String::AllocFromCharArray<true>(soa.Self(), char_count,
60 char_array, offset,
61 allocator_type);
62 return soa.AddLocalReference<jstring>(result);
63}
64
65static jstring StringFactory_newStringFromString(JNIEnv* env, jclass, jstring to_copy) {
66 ScopedFastNativeObjectAccess soa(env);
67 if (UNLIKELY(to_copy == nullptr)) {
68 ThrowNullPointerException("toCopy == null");
69 return nullptr;
70 }
71 StackHandleScope<1> hs(soa.Self());
72 Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String*>(to_copy)));
73 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
74 mirror::String* result = mirror::String::AllocFromString<true>(soa.Self(), string->GetLength(),
75 string, 0, allocator_type);
76 return soa.AddLocalReference<jstring>(result);
77}
78
79static JNINativeMethod gMethods[] = {
80 NATIVE_METHOD(StringFactory, newStringFromBytes, "!([BIII)Ljava/lang/String;"),
81 NATIVE_METHOD(StringFactory, newStringFromChars, "!(II[C)Ljava/lang/String;"),
82 NATIVE_METHOD(StringFactory, newStringFromString, "!(Ljava/lang/String;)Ljava/lang/String;"),
83};
84
85void register_java_lang_StringFactory(JNIEnv* env) {
86 REGISTER_NATIVE_METHODS("java/lang/StringFactory");
87}
88
89} // namespace art