diff options
author | Kenny Root <kroot@google.com> | 2010-05-07 11:01:27 -0700 |
---|---|---|
committer | Kenny Root <kroot@google.com> | 2010-05-07 11:02:04 -0700 |
commit | 2def26b3562f5fdb3a5c99f866d2aa2ee3fb361e (patch) | |
tree | 335f06226167735f4a676e3c75b3ced989bd4e7a /include/ScopedJavaUnicodeString.h | |
parent | b6f5c95cc2fcbed28f72db98512bb8caff55d857 (diff) | |
download | libcore-2def26b3562f5fdb3a5c99f866d2aa2ee3fb361e.zip libcore-2def26b3562f5fdb3a5c99f866d2aa2ee3fb361e.tar.gz libcore-2def26b3562f5fdb3a5c99f866d2aa2ee3fb361e.tar.bz2 |
Move ScopedJavaUnicodeString to include/
Change-Id: I51a2cdd80e910996f3143a7b699c3d4b408d105a
Diffstat (limited to 'include/ScopedJavaUnicodeString.h')
-rw-r--r-- | include/ScopedJavaUnicodeString.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/include/ScopedJavaUnicodeString.h b/include/ScopedJavaUnicodeString.h new file mode 100644 index 0000000..b108a6b --- /dev/null +++ b/include/ScopedJavaUnicodeString.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCOPED_JAVA_UNICODE_STRING_H_included +#define SCOPED_JAVA_UNICODE_STRING_H_included + +#include "JNIHelp.h" +#include "unicode/unistr.h" + +// A smart pointer that provides access to an ICU UnicodeString given a JNI +// jstring. We give ICU a direct pointer to the characters on the Java heap. +// It's clever enough to copy-on-write if necessary. +class ScopedJavaUnicodeString { +public: + ScopedJavaUnicodeString(JNIEnv* env, jstring s) : mEnv(env), mString(s) { + mChars = env->GetStringChars(mString, NULL); + const int32_t charCount = env->GetStringLength(mString); + mUnicodeString.setTo(false, mChars, charCount); + } + + ~ScopedJavaUnicodeString() { + mEnv->ReleaseStringChars(mString, mChars); + } + + const UnicodeString& unicodeString() const { + return mUnicodeString; + } + + UnicodeString& unicodeString() { + return mUnicodeString; + } + +private: + JNIEnv* mEnv; + jstring mString; + const UChar* mChars; + UnicodeString mUnicodeString; + + // Disallow copy and assignment. + ScopedJavaUnicodeString(const ScopedJavaUnicodeString&); + void operator=(const ScopedJavaUnicodeString&); +}; + +#endif // SCOPED_JAVA_UNICODE_STRING_H_included |