• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 Google Inc. All Rights Reserved.
2 
3    Distributed under MIT license.
4    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6 
7 #include <jni.h>
8 
9 #include "c/common/dictionary.h"
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /**
16  * Set data to be brotli dictionary data.
17  *
18  * @param buffer direct ByteBuffer
19  * @returns false if dictionary data was already set; otherwise true
20  */
21 JNIEXPORT jint JNICALL
Java_org_brotli_wrapper_common_CommonJNI_nativeSetDictionaryData(JNIEnv * env,jobject,jobject buffer)22 Java_org_brotli_wrapper_common_CommonJNI_nativeSetDictionaryData(
23     JNIEnv* env, jobject /*jobj*/, jobject buffer) {
24   jobject buffer_ref = env->NewGlobalRef(buffer);
25   if (!buffer_ref) {
26     return false;
27   }
28   uint8_t* data = static_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
29   if (!data) {
30     env->DeleteGlobalRef(buffer_ref);
31     return false;
32   }
33 
34   BrotliSetDictionaryData(data);
35 
36   const BrotliDictionary* dictionary = BrotliGetDictionary();
37   if (dictionary->data != data) {
38     env->DeleteGlobalRef(buffer_ref);
39   } else {
40     /* Don't release reference; it is an intended memory leak. */
41   }
42   return true;
43 }
44 
45 #ifdef __cplusplus
46 }
47 #endif
48