blob: 9d2ebccc8b0f3f17622712293cf78be1e8b87e4c [file] [log] [blame]
jat@google.com64a55cb2009-10-16 14:16:57 +00001/*
2 * Copyright 2007 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17#ifndef JNI_LINUX_JSTRINGWRAP_H_
18#define JNI_LINUX_JSTRINGWRAP_H_
19
20/*
21 * Wrapper arouond Java Strings, keeps pointers to unpacked strings
22 * and makes sure they are cleaned up.
23 */
24class JStringWrap {
25 public:
26 JStringWrap(JNIEnv* env, jstring str): env(env), s(str), p(0), jp(0) { }
27 ~JStringWrap() {
28 if (p) env->ReleaseStringUTFChars(s, p);
29 if (jp) env->ReleaseStringChars(s, jp);
30 }
31 const char* str() {
32 if (!p) p = env->GetStringUTFChars(s, 0);
33 return p;
34 }
35 const jchar* jstr() {
36 if (!jp) jp = env->GetStringChars(s, 0);
37 return jp;
38 }
39 int length() {
40 return env->GetStringLength(s);
41 }
42 private:
43 JNIEnv* env;
44 jstring s;
45 const char* p;
46 const jchar* jp;
47};
48
49
50#endif // JNI_LINUX_JSTRINGWRAP_H_