mirror of
https://github.com/google/brotli.git
synced 2024-11-21 19:20:09 +00:00
Java wrapper: allow using partial byte arrays (#999)
This commit is contained in:
parent
0ea4603880
commit
e3ea91d5c9
@ -138,16 +138,14 @@ public class Decoder {
|
|||||||
source.close();
|
source.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Decodes the given data buffer starting at offset till length. */
|
||||||
* Decodes the given data buffer.
|
public static byte[] decompress(byte[] data, int offset, int length) throws IOException {
|
||||||
*/
|
DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(length);
|
||||||
public static byte[] decompress(byte[] data) throws IOException {
|
|
||||||
DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(data.length);
|
|
||||||
ArrayList<byte[]> output = new ArrayList<byte[]>();
|
ArrayList<byte[]> output = new ArrayList<byte[]>();
|
||||||
int totalOutputSize = 0;
|
int totalOutputSize = 0;
|
||||||
try {
|
try {
|
||||||
decoder.getInputBuffer().put(data);
|
decoder.getInputBuffer().put(data, offset, length);
|
||||||
decoder.push(data.length);
|
decoder.push(length);
|
||||||
while (decoder.getStatus() != DecoderJNI.Status.DONE) {
|
while (decoder.getStatus() != DecoderJNI.Status.DONE) {
|
||||||
switch (decoder.getStatus()) {
|
switch (decoder.getStatus()) {
|
||||||
case OK:
|
case OK:
|
||||||
@ -182,11 +180,16 @@ public class Decoder {
|
|||||||
return output.get(0);
|
return output.get(0);
|
||||||
}
|
}
|
||||||
byte[] result = new byte[totalOutputSize];
|
byte[] result = new byte[totalOutputSize];
|
||||||
int offset = 0;
|
int resultOffset = 0;
|
||||||
for (byte[] chunk : output) {
|
for (byte[] chunk : output) {
|
||||||
System.arraycopy(chunk, 0, result, offset, chunk.length);
|
System.arraycopy(chunk, 0, result, resultOffset, chunk.length);
|
||||||
offset += chunk.length;
|
resultOffset += chunk.length;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Decodes the given data buffer. */
|
||||||
|
public static byte[] decompress(byte[] data) throws IOException {
|
||||||
|
return decompress(data, 0, data.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
|
|
||||||
package org.brotli.wrapper.enc;
|
package org.brotli.wrapper.enc;
|
||||||
|
|
||||||
import org.brotli.enc.PreparedDictionary;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.WritableByteChannel;
|
import java.nio.channels.WritableByteChannel;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.brotli.enc.PreparedDictionary;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for OutputStream / Channel implementations.
|
* Base class for OutputStream / Channel implementations.
|
||||||
@ -209,22 +209,21 @@ public class Encoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Encodes the given data buffer. */
|
||||||
* Encodes the given data buffer.
|
public static byte[] compress(byte[] data, int offset, int length, Parameters params)
|
||||||
*/
|
throws IOException {
|
||||||
public static byte[] compress(byte[] data, Parameters params) throws IOException {
|
if (length == 0) {
|
||||||
if (data.length == 0) {
|
|
||||||
byte[] empty = new byte[1];
|
byte[] empty = new byte[1];
|
||||||
empty[0] = 6;
|
empty[0] = 6;
|
||||||
return empty;
|
return empty;
|
||||||
}
|
}
|
||||||
/* data.length > 0 */
|
/* data.length > 0 */
|
||||||
EncoderJNI.Wrapper encoder =
|
EncoderJNI.Wrapper encoder =
|
||||||
new EncoderJNI.Wrapper(data.length, params.quality, params.lgwin, params.mode);
|
new EncoderJNI.Wrapper(length, params.quality, params.lgwin, params.mode);
|
||||||
ArrayList<byte[]> output = new ArrayList<byte[]>();
|
ArrayList<byte[]> output = new ArrayList<byte[]>();
|
||||||
int totalOutputSize = 0;
|
int totalOutputSize = 0;
|
||||||
try {
|
try {
|
||||||
encoder.getInputBuffer().put(data);
|
encoder.getInputBuffer().put(data, offset, length);
|
||||||
encoder.push(EncoderJNI.Operation.FINISH, data.length);
|
encoder.push(EncoderJNI.Operation.FINISH, data.length);
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!encoder.isSuccess()) {
|
if (!encoder.isSuccess()) {
|
||||||
@ -248,18 +247,27 @@ public class Encoder {
|
|||||||
return output.get(0);
|
return output.get(0);
|
||||||
}
|
}
|
||||||
byte[] result = new byte[totalOutputSize];
|
byte[] result = new byte[totalOutputSize];
|
||||||
int offset = 0;
|
int resultOffset = 0;
|
||||||
for (byte[] chunk : output) {
|
for (byte[] chunk : output) {
|
||||||
System.arraycopy(chunk, 0, result, offset, chunk.length);
|
System.arraycopy(chunk, 0, result, resultOffset, chunk.length);
|
||||||
offset += chunk.length;
|
resultOffset += chunk.length;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Encodes the given data buffer. */
|
||||||
|
public static byte[] compress(byte[] data, Parameters params) throws IOException {
|
||||||
|
return compress(data, 0, data.length, params);
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] compress(byte[] data) throws IOException {
|
public static byte[] compress(byte[] data) throws IOException {
|
||||||
return compress(data, new Parameters());
|
return compress(data, new Parameters());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] compress(byte[] data, int offset, int length) throws IOException {
|
||||||
|
return compress(data, offset, length, new Parameters());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepares raw or serialized dictionary for being used by encoder.
|
* Prepares raw or serialized dictionary for being used by encoder.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user