blob: e17981fad30bfbf4188c64603339bffa24ba8ec7 [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#ifndef JSTRINGWRAP_H
17#define JSTRINGWRAP_H
18
19#include <jni.h>
20
21/*
22 * Wrap a Java String and automatically clean up temporary storage allocated
23 * for accessing its contents.
24 */
25struct JStringWrap
26{
27 JStringWrap(JNIEnv* env, jstring str): env(env), s(str), p(0), jp(0) { }
28 ~JStringWrap() {
29 if (p) env->ReleaseStringUTFChars(s, p);
30 if (jp) env->ReleaseStringChars(s, jp);
31 }
32 const char* str() { if (!p) p = env->GetStringUTFChars(s, 0); return p; }
33 const jchar* jstr() { if (!jp) jp = env->GetStringChars(s, 0); return jp; }
34 jsize length() { return env->GetStringLength(s); }
35private:
36 JNIEnv* env;
37 jstring s;
38 const char* p;
39 const jchar* jp;
40};
41
42#endif