Updating JNI modules of service provider
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_resource_jni_util.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 #include "simulator_resource_jni_util.h"
21
22 std::vector<int> convertIntegerVector(JNIEnv *env, jobject jVectorInt)
23 {
24     std::vector<int> vectorInt;
25
26     jclass vectorClass = env->FindClass("java/util/Vector");
27     if (!vectorClass)
28     {
29         return vectorInt;
30     }
31
32     jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
33     if (NULL == size)
34     {
35         return vectorInt;
36     }
37
38     jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
39     if (NULL == get)
40     {
41         return vectorInt;
42     }
43
44     jint jSize = env->CallIntMethod(jVectorInt, size);
45     int sizeOfVector = jSize;
46
47     for (int index = 0; index < sizeOfVector; index++)
48     {
49         jint jIndex = index;
50         jint jValue = env->CallIntMethod(jVectorInt, get, jIndex);
51         vectorInt.push_back((int)jValue);
52     }
53
54     return vectorInt;
55 }
56
57 std::vector<double> convertDoubleVector(JNIEnv *env, jobject jVectorDouble)
58 {
59     std::vector<double> vectorDouble;
60
61     jclass vectorClass = env->FindClass("java/util/Vector");
62     if (!vectorClass)
63     {
64         return vectorDouble;
65     }
66
67     jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
68     if (NULL == size)
69     {
70         return vectorDouble;
71     }
72
73     jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
74     if (NULL == get)
75     {
76         return vectorDouble;
77     }
78
79     jint jSize = env->CallIntMethod(jVectorDouble, size);
80     int sizeOfVector = jSize;
81
82     for (int index = 0; index < sizeOfVector; index++)
83     {
84         jint jIndex = index;
85         jdouble jValue = env->CallDoubleMethod(jVectorDouble, get, jIndex);
86         vectorDouble.push_back((double)jValue);
87     }
88
89     return vectorDouble;
90 }
91
92 std::vector<std::string> convertStringVector(JNIEnv *env, jobject jVectorString)
93 {
94     std::vector<std::string> vectorString;
95
96     jclass vectorClass = env->FindClass("java/util/Vector");
97     if (!vectorClass)
98     {
99         return vectorString;
100     }
101
102     jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
103     if (NULL == size)
104     {
105         return vectorString;
106     }
107
108     jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
109     if (NULL == get)
110     {
111         return vectorString;
112     }
113
114     jint jSize = env->CallIntMethod(jVectorString, size);
115     int sizeOfVector = jSize;
116
117     for (int index = 0; index < sizeOfVector; index++)
118     {
119         jint jIndex = index;
120         jstring jContactInfoObj = (jstring)env->CallObjectMethod(jVectorString, get, jIndex);
121         if (jContactInfoObj == NULL)
122         {
123             return vectorString;
124         }
125         const char *buff = env->GetStringUTFChars(jContactInfoObj, 0);
126         if (NULL != buff)
127         {
128             std::string tempString = buff;
129             vectorString.push_back(tempString);
130         }
131
132         env->ReleaseStringUTFChars(jContactInfoObj, buff);
133     }
134
135     return vectorString;
136 }