-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompressor.c
128 lines (110 loc) · 5.64 KB
/
compressor.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "./common.h"
#include "./jni_util.h"
#include "./libdeflate/libdeflate.h"
LIBDEFLATEJAVA_PUBLIC JNIEXPORT jlong JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_allocate(JNIEnv *env, jclass klass, jint level) {
struct libdeflate_compressor *compressor = libdeflate_alloc_compressor(level);
if (compressor == NULL) {
// Out of memory!
throwException(env, "java/lang/OutOfMemoryError", "libdeflate allocate compressor");
return 0;
}
return (jlong) compressor;
}
LIBDEFLATEJAVA_PUBLIC JNIEXPORT void JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_free(JNIEnv *env, jclass klass, jlong ctx) {
libdeflate_free_compressor((struct libdeflate_compressor *) ctx);
}
jlong performCompression(jlong ctx, jbyte* inBytes, jint inPos, jint inSize, jbyte* outBytes, jint outPos, jint outSize, jint type) {
// We assume that any input validation has already been done before the method has been called.
struct libdeflate_compressor *compressor = (struct libdeflate_compressor *) ctx;
void *inStart = (void *) (inBytes + inPos);
void *outStart = (void *) (outBytes + outPos);
size_t result = 0;
switch (type) {
case COMPRESSION_TYPE_DEFLATE:
result = libdeflate_deflate_compress(compressor, inStart, inSize, outStart, outSize);
break;
case COMPRESSION_TYPE_ZLIB:
result = libdeflate_zlib_compress(compressor, inStart, inSize, outStart, outSize);
break;
case COMPRESSION_TYPE_GZIP:
result = libdeflate_gzip_compress(compressor, inStart, inSize, outStart, outSize);
break;
}
return (jlong) result;
}
LIBDEFLATEJAVA_PUBLIC JNIEXPORT jlong JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_compressBothHeap
(JNIEnv *env, jclass klass, jlong ctx, jbyteArray in, jint inPos, jint inSize, jbyteArray out, jint outPos, jint outSize, jint type) {
jbyte *inBytes = (*env)->GetPrimitiveArrayCritical(env, in, 0);
jbyte *outBytes = (*env)->GetPrimitiveArrayCritical(env, out, 0);
if (inBytes == NULL || outBytes == NULL) {
if (inBytes != NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, in, inBytes, JNI_ABORT);
}
return -1;
}
jlong result = performCompression(ctx, inBytes, inPos, inSize, outBytes, outPos, outSize, type);
// We immediately commit the changes to the output array, but the input array is never touched, so use JNI_ABORT
// to improve performance a bit.
(*env)->ReleasePrimitiveArrayCritical(env, in, inBytes, JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, in, outBytes, 0);
return (jint) result;
}
LIBDEFLATEJAVA_PUBLIC JNIEXPORT jlong JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_compressBothDirect
(JNIEnv *env, jclass klass, jlong ctx, jobject in, jint inPos, jint inSize, jobject out, jint outPos, jint outSize, jint type) {
jbyte *inBytes = (*env)->GetDirectBufferAddress(env, in);
jbyte *outBytes = (*env)->GetDirectBufferAddress(env, out);
if (inBytes == NULL || outBytes == NULL) {
throwException(env, "java/lang/IllegalArgumentException", "unable to obtain direct access to buffer");
return -1;
}
return performCompression(ctx, inBytes, inPos, inSize, outBytes, outPos, outSize, type);
}
LIBDEFLATEJAVA_PUBLIC JNIEXPORT jlong JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_compressOnlySourceDirect
(JNIEnv *env, jclass klass, jlong ctx, jobject in, jint inPos, jint inSize, jbyteArray out, jint outPos, jint outSize, jint type) {
jbyte *inBytes = (*env)->GetDirectBufferAddress(env, in);
if (inBytes == NULL) {
throwException(env, "java/lang/IllegalArgumentException", "unable to obtain direct access to input buffer");
return -1;
}
jbyte *outBytes = (*env)->GetPrimitiveArrayCritical(env, out, 0);
if (outBytes == NULL) {
// out of memory
return -1;
}
jlong result = performCompression(ctx, inBytes, inPos, inSize, outBytes, outPos, outSize, type);
// Commit the output array
(*env)->ReleasePrimitiveArrayCritical(env, out, outBytes, 0);
return result;
}
LIBDEFLATEJAVA_PUBLIC JNIEXPORT jlong JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_compressOnlyDestinationDirect
(JNIEnv *env, jclass klass, jlong ctx, jbyteArray in, jint inPos, jint inSize, jobject out, jint outPos, jint outSize, jint type) {
jbyte *outBytes = (*env)->GetDirectBufferAddress(env, out);
if (outBytes == NULL) {
throwException(env, "java/lang/IllegalArgumentException", "unable to obtain direct access to output buffer");
return -1;
}
jbyte *inBytes = (*env)->GetPrimitiveArrayCritical(env, in, 0);
if (outBytes == NULL) {
// out of memory
return -1;
}
jlong result = performCompression(ctx, inBytes, inPos, inSize, outBytes, outPos, outSize, type);
(*env)->ReleasePrimitiveArrayCritical(env, in, inBytes, JNI_ABORT);
return result;
}
LIBDEFLATEJAVA_PUBLIC JNIEXPORT jlong JNICALL Java_me_steinborn_libdeflate_LibdeflateCompressor_getCompressBound(JNIEnv *env, jclass klass, jlong ctx, jlong length, jint type) {
struct libdeflate_compressor *compressor = (struct libdeflate_compressor *) ctx;
size_t result = 0;
switch (type) {
case COMPRESSION_TYPE_DEFLATE:
result = libdeflate_deflate_compress_bound(compressor, length);
break;
case COMPRESSION_TYPE_ZLIB:
result = libdeflate_zlib_compress_bound(compressor, length);
break;
case COMPRESSION_TYPE_GZIP:
result = libdeflate_gzip_compress_bound(compressor, length);
break;
}
return (jlong) result;
}