aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/wrapper/common/common_jni.cc
blob: 2ea2ad8707c04a3f140df3e8446bc1d4153113cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Copyright 2017 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

#include <jni.h>

#include "../common/dictionary.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Set data to be brotli dictionary data.
 *
 * @param buffer direct ByteBuffer
 * @returns false if dictionary data was already set; otherwise true
 */
JNIEXPORT jint JNICALL
Java_org_brotli_wrapper_common_CommonJNI_nativeSetDictionaryData(
    JNIEnv* env, jobject /*jobj*/, jobject buffer) {
  jobject buffer_ref = env->NewGlobalRef(buffer);
  if (!buffer_ref) {
    return false;
  }
  uint8_t* data = static_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
  if (!data) {
    env->DeleteGlobalRef(buffer_ref);
    return false;
  }

  BrotliSetDictionaryData(data);

  const BrotliDictionary* dictionary = BrotliGetDictionary();
  if (dictionary->data != data) {
    env->DeleteGlobalRef(buffer_ref);
  } else {
    /* Don't release reference; it is an intended memory leak. */
  }
  return true;
}

#ifdef __cplusplus
}
#endif