OR-Tools  8.1
jniutil.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_JNIUTIL_H_
15 #define OR_TOOLS_BASE_JNIUTIL_H_
16 
17 #include <jni.h>
18 
19 #include <string>
20 
21 #include "ortools/base/logging.h"
22 
23 class JNIUtil {
24  public:
25  // Creates a Java jstring from a null-terminated UTF-8 encoded C String.
26  // The caller must delete the jstring reference.
27  static jstring MakeJString(JNIEnv* env, const char* cstr) {
28  if (cstr == NULL) return NULL;
29  return env->NewStringUTF(cstr);
30  }
31 
32  // Creates a null-terminated UTF-8 encoded C string from a jstring.
33  // The returned string should be "delete[]"-ed when no longer needed.
34  static char* MakeCString(JNIEnv* env, jstring str) {
35  if (str == NULL) return NULL;
36  jsize length = env->GetStringUTFLength(str);
37  const char* src = env->GetStringUTFChars(str, NULL);
38  char* dst = new char[length + 1];
39  memcpy(dst, src, length);
40  dst[length] = '\0';
41  env->ReleaseStringUTFChars(str, src);
42  return dst;
43  }
44 
45  // Creates a new char array from a jbyteArray.
46  // The caller must delete[] the returned array.
47  static char* MakeCharArray(JNIEnv* env, jbyteArray a, int* size) {
48  jsize n = env->GetArrayLength(a);
49  *size = n;
50  jbyte* jba = new jbyte[n];
51 
52  env->GetByteArrayRegion(a, 0, n, jba);
53  // We make use of the fact that jbyte's are really just chars.
54  // If this changes (different VM, etc.) things will break.
55  return reinterpret_cast<char*>(jba);
56  }
57 
58  // Produces a jbyteArray from a char array.
59  static jbyteArray MakeJByteArray(JNIEnv* env, const char* a, int size) {
60  // Create empty array object
61  jbyteArray output = env->NewByteArray(size);
62  // Fill it
63  env->SetByteArrayRegion(output, 0, size, reinterpret_cast<const jbyte*>(a));
64  return output;
65  }
66 };
67 
68 #endif // OR_TOOLS_BASE_JNIUTIL_H_
logging.h
JNIUtil::MakeCString
static char * MakeCString(JNIEnv *env, jstring str)
Definition: jniutil.h:34
JNIUtil
Definition: jniutil.h:23
JNIUtil::MakeCharArray
static char * MakeCharArray(JNIEnv *env, jbyteArray a, int *size)
Definition: jniutil.h:47
a
int64 a
Definition: constraint_solver/table.cc:42
JNIUtil::MakeJByteArray
static jbyteArray MakeJByteArray(JNIEnv *env, const char *a, int size)
Definition: jniutil.h:59
JNIUtil::MakeJString
static jstring MakeJString(JNIEnv *env, const char *cstr)
Definition: jniutil.h:27