diff options
Diffstat (limited to 'java/org/brotli/wrapper/enc/EncoderJNI.java')
-rw-r--r-- | java/org/brotli/wrapper/enc/EncoderJNI.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/java/org/brotli/wrapper/enc/EncoderJNI.java b/java/org/brotli/wrapper/enc/EncoderJNI.java index d0c5fac..1cbd7c1 100644 --- a/java/org/brotli/wrapper/enc/EncoderJNI.java +++ b/java/org/brotli/wrapper/enc/EncoderJNI.java @@ -6,6 +6,7 @@ package org.brotli.wrapper.enc; +import org.brotli.enc.PreparedDictionary; import java.io.IOException; import java.nio.ByteBuffer; @@ -17,6 +18,9 @@ class EncoderJNI { private static native void nativePush(long[] context, int length); private static native ByteBuffer nativePull(long[] context); private static native void nativeDestroy(long[] context); + private static native boolean nativeAttachDictionary(long[] context, ByteBuffer dictionary); + private static native ByteBuffer nativePrepareDictionary(ByteBuffer dictionary, long type); + private static native void nativeDestroyDictionary(ByteBuffer dictionary); enum Operation { PROCESS, @@ -24,6 +28,47 @@ class EncoderJNI { FINISH } + private static class PreparedDictionaryImpl implements PreparedDictionary { + private ByteBuffer data; + + private PreparedDictionaryImpl(ByteBuffer data) { + this.data = data; + } + + @Override + public ByteBuffer getData() { + return data; + } + + @Override + protected void finalize() throws Throwable { + try { + ByteBuffer data = this.data; + this.data = null; + nativeDestroyDictionary(data); + } finally { + super.finalize(); + } + } + } + + /** + * Prepares raw or serialized dictionary for being used by encoder. + * + * @param dictionary raw / serialized dictionary data; MUST be direct + * @param sharedDictionaryType dictionary data type + */ + static PreparedDictionary prepareDictionary(ByteBuffer dictionary, int sharedDictionaryType) { + if (!dictionary.isDirect()) { + throw new IllegalArgumentException("only direct buffers allowed"); + } + ByteBuffer dictionaryData = nativePrepareDictionary(dictionary, sharedDictionaryType); + if (dictionaryData == null) { + throw new IllegalStateException("OOM"); + } + return new PreparedDictionaryImpl(dictionaryData); + } + static class Wrapper { protected final long[] context = new long[5]; private final ByteBuffer inputBuffer; @@ -48,6 +93,19 @@ class EncoderJNI { this.context[4] = 0; } + boolean attachDictionary(ByteBuffer dictionary) { + if (!dictionary.isDirect()) { + throw new IllegalArgumentException("only direct buffers allowed"); + } + if (context[0] == 0) { + throw new IllegalStateException("brotli decoder is already destroyed"); + } + if (!fresh) { + throw new IllegalStateException("decoding is already started"); + } + return nativeAttachDictionary(context, dictionary); + } + void push(Operation op, int length) { if (length < 0) { throw new IllegalArgumentException("negative block length"); |