summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2011-02-07 15:55:24 -0800
committerElliott Hughes <enh@google.com>2011-02-07 15:55:24 -0800
commit37871fb106b08055ad56d7f04d4faccdd163e1af (patch)
treebd2a5299a1fb3b50c6e8714f7162ac1a6b80cf72 /include
parentfabe243c8c726ec213c3e4fce3b55c977480743f (diff)
downloadlibcore-37871fb106b08055ad56d7f04d4faccdd163e1af.zip
libcore-37871fb106b08055ad56d7f04d4faccdd163e1af.tar.gz
libcore-37871fb106b08055ad56d7f04d4faccdd163e1af.tar.bz2
Clean up the CharsetDecoder/CharsetEncoder implementation a bit more.
This removes the duplicated error-checking in the ICU classes that started this investigation, but also cleans up a bunch of other stuff. There shouldn't be any other behavioral differences, just clearer code. Bug: 3418769 Change-Id: I4da4d2a5b2fce1b152e527909b7c76a6db76c5c0
Diffstat (limited to 'include')
-rw-r--r--include/ScopedStringChars.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/include/ScopedStringChars.h b/include/ScopedStringChars.h
new file mode 100644
index 0000000..9f543b7
--- /dev/null
+++ b/include/ScopedStringChars.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2011 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_STRING_CHARS_H_included
+#define SCOPED_STRING_CHARS_H_included
+
+#include "JNIHelp.h"
+
+// A smart pointer that provides access to a jchar* given a JNI jstring.
+class ScopedStringChars {
+public:
+ ScopedStringChars(JNIEnv* env, jstring s) : mEnv(env), mString(s), mSize(0) {
+ mChars = env->GetStringChars(mString, NULL);
+ if (mChars != NULL) {
+ mSize = env->GetStringLength(mString);
+ }
+ }
+
+ ~ScopedStringChars() {
+ mEnv->ReleaseStringChars(mString, mChars);
+ }
+
+ const jchar* get() const { return mChars; }
+ const jchar& operator[](size_t n) const { return mChars[n]; }
+ size_t size() const { return mSize; }
+
+private:
+ JNIEnv* mEnv;
+ jstring mString;
+ const UChar* mChars;
+ size_t mSize;
+
+ // Disallow copy and assignment.
+ ScopedStringChars(const ScopedStringChars&);
+ void operator=(const ScopedStringChars&);
+};
+
+#endif // SCOPED_STRING_CHARS_H_included