1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <jni.h>
18 #include <string>
19 #include <search.h>
20 #include "generated/animal_generated.h"
21
22 using namespace com::fbs::app;
23 using namespace flatbuffers;
24
Java_com_flatbuffers_app_MainActivity_createAnimalFromJNI(JNIEnv * env,jobject)25 extern "C" JNIEXPORT jbyteArray JNICALL Java_com_flatbuffers_app_MainActivity_createAnimalFromJNI(
26 JNIEnv* env,
27 jobject /* this */) {
28 // create a new animal flatbuffers
29 auto fb = FlatBufferBuilder(1024);
30 auto tiger = CreateAnimalDirect(fb, "Tiger", "Roar", 300);
31 fb.Finish(tiger);
32
33 // copies it to a Java byte array.
34 auto buf = reinterpret_cast<jbyte*>(fb.GetBufferPointer());
35 int size = fb.GetSize();
36 auto ret = env->NewByteArray(size);
37 env->SetByteArrayRegion (ret, 0, fb.GetSize(), buf);
38 return ret;
39 }
40