[Java] make it possible to set modes (generic, text, font) (#887)

* [Java] make it possible to set modes (generic, text, font)
This commit is contained in:
Martin Grigorov 2021-03-24 22:23:03 +02:00 committed by GitHub
parent 2f9277ff2f
commit bbe5d72ba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 3 deletions

View File

@ -22,18 +22,47 @@ public class Encoder {
final ByteBuffer inputBuffer;
boolean closed;
/**
* https://www.brotli.org/encode.html#aa6f
* See encode.h, typedef enum BrotliEncoderMode
*
* <strong>Important</strong>: The ordinal value of the
* modes should be the same as the constant values in encode.h
*/
public enum Mode {
/**
* Default compression mode.
* In this mode compressor does not know anything in advance about the properties of the input.
*/
GENERIC,
/**
* Compression mode for UTF-8 formatted text input.
*/
TEXT,
/**
* Compression mode used in WOFF 2.0.
*/
FONT;
public static Mode of(int value) {
return values()[value];
}
}
/**
* Brotli encoder settings.
*/
public static final class Parameters {
private int quality = -1;
private int lgwin = -1;
private Mode mode;
public Parameters() { }
private Parameters(Parameters other) {
this.quality = other.quality;
this.lgwin = other.lgwin;
this.mode = other.mode;
}
/**
@ -57,6 +86,14 @@ public class Encoder {
this.lgwin = lgwin;
return this;
}
/**
* @param mode compression mode, or {@code null} for default
*/
public Parameters setMode(Mode mode) {
this.mode = mode;
return this;
}
}
/**
@ -75,7 +112,7 @@ public class Encoder {
throw new NullPointerException("destination can not be null");
}
this.destination = destination;
this.encoder = new EncoderJNI.Wrapper(inputBufferSize, params.quality, params.lgwin);
this.encoder = new EncoderJNI.Wrapper(inputBufferSize, params.quality, params.lgwin, params.mode);
this.inputBuffer = this.encoder.getInputBuffer();
}
@ -163,7 +200,7 @@ public class Encoder {
return empty;
}
/* data.length > 0 */
EncoderJNI.Wrapper encoder = new EncoderJNI.Wrapper(data.length, params.quality, params.lgwin);
EncoderJNI.Wrapper encoder = new EncoderJNI.Wrapper(data.length, params.quality, params.lgwin, params.mode);
ArrayList<byte[]> output = new ArrayList<byte[]>();
int totalOutputSize = 0;
try {

View File

@ -29,7 +29,7 @@ class EncoderJNI {
private final ByteBuffer inputBuffer;
private boolean fresh = true;
Wrapper(int inputBufferSize, int quality, int lgwin)
Wrapper(int inputBufferSize, int quality, int lgwin, Encoder.Mode mode)
throws IOException {
if (inputBufferSize <= 0) {
throw new IOException("buffer size must be positive");
@ -37,6 +37,7 @@ class EncoderJNI {
this.context[1] = inputBufferSize;
this.context[2] = quality;
this.context[3] = lgwin;
this.context[4] = mode != null ? mode.ordinal() : -1;
this.inputBuffer = nativeCreate(this.context);
if (this.context[0] == 0) {
throw new IOException("failed to initialize native brotli encoder");
@ -44,6 +45,7 @@ class EncoderJNI {
this.context[1] = 1;
this.context[2] = 0;
this.context[3] = 0;
this.context[4] = 0;
}
void push(Operation op, int length) {

View File

@ -79,6 +79,10 @@ Java_org_brotli_wrapper_enc_EncoderJNI_nativeCreate(
if (lgwin >= 0) {
BrotliEncoderSetParameter(handle->state, BROTLI_PARAM_LGWIN, lgwin);
}
int mode = context[4];
if (mode >= 0) {
BrotliEncoderSetParameter(handle->state, BROTLI_PARAM_MODE, mode);
}
}
if (ok) {