MCPcopy
hub / github.com/google/brotli / compress

Method compress

java/org/brotli/wrapper/enc/Encoder.java:211–254  ·  view source on GitHub ↗

Encodes the given data buffer.

(byte[] data, int offset, int length, Parameters params)

Source from the content-addressed store, hash-verified

209
210 /** Encodes the given data buffer. */
211 public static byte[] compress(byte[] data, int offset, int length, Parameters params)
212 throws IOException {
213 if (length == 0) {
214 byte[] empty = new byte[1];
215 empty[0] = 6;
216 return empty;
217 }
218 /* data.length > 0 */
219 ArrayList<byte[]> output = new ArrayList<>();
220 int totalOutputSize = 0;
221 EncoderJNI.Wrapper encoder =
222 new EncoderJNI.Wrapper(length, params.quality, params.lgwin, params.mode);
223 try {
224 encoder.getInputBuffer().put(data, offset, length);
225 encoder.push(EncoderJNI.Operation.FINISH, length);
226 while (true) {
227 if (!encoder.isSuccess()) {
228 throw new IOException("encoding failed");
229 } else if (encoder.hasMoreOutput()) {
230 ByteBuffer buffer = encoder.pull();
231 byte[] chunk = new byte[buffer.remaining()];
232 buffer.get(chunk);
233 output.add(chunk);
234 totalOutputSize += chunk.length;
235 } else if (!encoder.isFinished()) {
236 encoder.push(EncoderJNI.Operation.FINISH, 0);
237 } else {
238 break;
239 }
240 }
241 } finally {
242 encoder.destroy();
243 }
244 if (output.size() == 1) {
245 return output.get(0);
246 }
247 byte[] result = new byte[totalOutputSize];
248 int resultOffset = 0;
249 for (byte[] chunk : output) {
250 System.arraycopy(chunk, 0, result, resultOffset, chunk.length);
251 resultOffset += chunk.length;
252 }
253 return result;
254 }
255
256 /** Encodes the given data buffer. */
257 public static byte[] compress(byte[] data, Parameters params) throws IOException {

Callers 11

deepThoughtMethod · 0.95
testPowerOfTwoInputMethod · 0.95
testInputOffsetMethod · 0.95
testEmptyInputMethod · 0.95
runMethod · 0.95
brotlidump.pyFile · 0.80
mainFunction · 0.80
test_garbage_appendedMethod · 0.80
_compressMethod · 0.80
test_garbage_appendedMethod · 0.80
test_already_finishedMethod · 0.80

Calls 8

getInputBufferMethod · 0.95
pushMethod · 0.95
isSuccessMethod · 0.95
hasMoreOutputMethod · 0.95
pullMethod · 0.95
isFinishedMethod · 0.95
destroyMethod · 0.95
addMethod · 0.80

Tested by 8

testPowerOfTwoInputMethod · 0.76
testInputOffsetMethod · 0.76
testEmptyInputMethod · 0.76
runMethod · 0.76
test_garbage_appendedMethod · 0.64
_compressMethod · 0.64
test_garbage_appendedMethod · 0.64
test_already_finishedMethod · 0.64