Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 3 | * |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 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 |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 7 | * |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 9 | * |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 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 | #ifndef SCOPED_JAVA_UNICODE_STRING_H_included |
| 18 | #define SCOPED_JAVA_UNICODE_STRING_H_included |
| 19 | |
| 20 | #include "JNIHelp.h" |
Elliott Hughes | a656b24 | 2010-03-01 16:28:05 -0800 | [diff] [blame] | 21 | #include "unicode/unistr.h" |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 22 | |
| 23 | // A smart pointer that provides access to an ICU UnicodeString given a JNI |
| 24 | // jstring. We give ICU a direct pointer to the characters on the Java heap. |
Elliott Hughes | 9de899c | 2010-04-12 18:32:50 -0700 | [diff] [blame] | 25 | // It's clever enough to copy-on-write if necessary. |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 26 | class ScopedJavaUnicodeString { |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 27 | public: |
| 28 | ScopedJavaUnicodeString(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
| 29 | if (s == NULL) { |
| 30 | jniThrowNullPointerException(mEnv, NULL); |
| 31 | } else { |
| 32 | mChars = env->GetStringChars(mString, NULL); |
| 33 | const int32_t charCount = env->GetStringLength(mString); |
| 34 | mUnicodeString.setTo(false, mChars, charCount); |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 35 | } |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 36 | } |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 38 | ~ScopedJavaUnicodeString() { |
| 39 | if (mString != NULL) { |
| 40 | mEnv->ReleaseStringChars(mString, mChars); |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 41 | } |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 42 | } |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 43 | |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 44 | bool valid() const { |
| 45 | return (mString != NULL); |
| 46 | } |
Elliott Hughes | 9de899c | 2010-04-12 18:32:50 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 48 | const UnicodeString& unicodeString() const { |
| 49 | return mUnicodeString; |
| 50 | } |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 51 | |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 52 | UnicodeString& unicodeString() { |
| 53 | return mUnicodeString; |
| 54 | } |
Elliott Hughes | 7ca6fd0 | 2010-03-29 20:51:48 -0700 | [diff] [blame] | 55 | |
Elliott Hughes | 3aac4dd | 2013-02-04 15:37:52 -0800 | [diff] [blame] | 56 | private: |
| 57 | JNIEnv* mEnv; |
| 58 | jstring mString; |
| 59 | const UChar* mChars; |
| 60 | UnicodeString mUnicodeString; |
| 61 | |
| 62 | // Disallow copy and assignment. |
| 63 | ScopedJavaUnicodeString(const ScopedJavaUnicodeString&); |
| 64 | void operator=(const ScopedJavaUnicodeString&); |
Elliott Hughes | d5344fe | 2010-01-28 12:18:39 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | #endif // SCOPED_JAVA_UNICODE_STRING_H_included |