Java : Deflater (ZIP) with Examples

Deflater (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Deflater methods.


Summary

This class provides support for general purpose compression using the popular ZLIB compression library. The ZLIB compression library was initially developed as part of the PNG graphics standard and is not protected by patents.

Class diagram

Please see also : DeflaterInputStream, DeflaterOutputStream

// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[15];
int compressedLength;
try {
    final var input = "aaaaa".getBytes();

    // [97, 97, 97, 97, 97]
    System.out.println(Arrays.toString(input));

    deflater.setInput(input);
    deflater.finish();

    compressedLength = deflater.deflate(compressed);

    // 11
    System.out.println(compressedLength);

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

// --------
// Decompression
final var inflater = new Inflater();
try {
    inflater.setInput(compressed, 0, compressedLength);

    final var decompressed = new byte[10];
    final var decompressedLength = inflater.inflate(decompressed);

    // 5
    System.out.println(decompressedLength);

    // [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(decompressed));

    // aaaaa
    System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
    inflater.end();
}
final var deflater = new Deflater();
try {
    final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();

    deflater.setInput(input);
    deflater.finish();

    while (!deflater.finished()) {
        final var compressed = new byte[10];
        final var length = deflater.deflate(compressed);
        System.out.printf("length=%2d : %s%n", length, Arrays.toString(compressed));
    }
} finally {
    deflater.end();
}

// Result
// ↓
//length=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]

Fields

static final int BEST_COMPRESSION

Compression level for best compression.

Please see Deflater(int level).

static final int BEST_SPEED

Compression level for fastest compression.

Please see Deflater(int level).

static final int DEFAULT_COMPRESSION

Default compression level.

Please see Deflater(int level).

static final int DEFAULT_STRATEGY

Default compression strategy.

Please see setStrategy(int strategy).

static final int DEFLATED

Compression method for the deflate algorithm (the only one currently supported).

Related Fields : ZipEntry.DEFLATED, ZipEntry.STORED

System.out.println(Deflater.DEFLATED); // 8

static final int FILTERED

Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.

Please see setStrategy(int strategy).

static final int FULL_FLUSH

Compression flush mode used to flush out all pending output and reset the deflater.

Please see deflate(byte[] output, int off, int len, int flush).

static final int HUFFMAN_ONLY

Compression strategy for Huffman coding only.

Please see setStrategy(int strategy).

static final int NO_COMPRESSION

Compression level for no compression.

Please see Deflater(int level).

static final int NO_FLUSH

Compression flush mode used to achieve best compression result.

Please see deflate(byte[] output, int off, int len, int flush).

static final int SYNC_FLUSH

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Please see deflate(byte[] output, int off, int len, int flush).

Constructors

Deflater ()

Creates a new compressor with the default compression level.

final var deflater = new Deflater();
try {
    final var input = "aaaaa".getBytes();

    // [97, 97, 97, 97, 97]
    System.out.println(Arrays.toString(input));

    deflater.setInput(input);
    deflater.finish();

    final var compressed = new byte[15];
    final var length = deflater.deflate(compressed);

    // 11
    System.out.println(length);

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

Deflater (int level)

Creates a new compressor using the specified compression level.

byte[] deflate(byte[] input, int level) {
    final var deflater = new Deflater(level);
    try {
        deflater.setInput(input);
        deflater.finish();

        final var compressed = new byte[100];
        final var length = deflater.deflate(compressed);

        return Arrays.copyOf(compressed, length);
    } finally {
        deflater.end();
    }
}
final var input = "aaaaXXXX".getBytes();

// [97, 97, 97, 97, 88, 88, 88, 88]
System.out.println(Arrays.toString(input));

{
    final var compressed = deflate(input, Deflater.DEFAULT_COMPRESSION);

    // [120, -100, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
    System.out.println(Arrays.toString(compressed));
}
{
    final var compressed = deflate(input, Deflater.NO_COMPRESSION);

    // [120, 1, 1, 8, 0, -9, -1, 97, 97, 97, 97, 88, 88, 88, 88, 13, 82, 2, -27]
    System.out.println(Arrays.toString(compressed));
}
{
    final var compressed = deflate(input, Deflater.BEST_SPEED);

    // [120, 1, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
    System.out.println(Arrays.toString(compressed));
}
{
    final var compressed = deflate(input, Deflater.BEST_COMPRESSION);

    // [120, -38, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
    System.out.println(Arrays.toString(compressed));
}

Deflater (int level, boolean nowrap)

Creates a new compressor using the specified compression level.

Please see Deflater(int level) for level parameter.

final var deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
final var compressed = new byte[10];
int compressLength;
try {
    final var input = "aaaXXX".getBytes();

    // [97, 97, 97, 88, 88, 88]
    System.out.println(Arrays.toString(input));

    deflater.setInput(input);
    deflater.finish();

    compressLength = deflater.deflate(compressed);

    // 8
    System.out.println(compressLength);

    // [75, 76, 76, -116, -120, -120, 0, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

final var inflater = new Inflater(true);
try {
    inflater.setInput(compressed, 0, compressLength);

    final var uncompressed = new byte[10];
    final var uncompressedLength = inflater.inflate(uncompressed);

    // 6
    System.out.println(uncompressedLength);

    // [97, 97, 97, 88, 88, 88, 0, 0, 0, 0]
    System.out.println(Arrays.toString(uncompressed));

    // aaaXXX
    System.out.println(new String(uncompressed, 0, uncompressedLength));
} finally {
    inflater.end();
}

Methods

int deflate (byte[] output)

Compresses the input data and fills specified buffer with compressed data.

final var deflater = new Deflater();
try {
    final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();

    deflater.setInput(input);
    deflater.finish();

    while (!deflater.finished()) {
        final var compressed = new byte[10];
        final var length = deflater.deflate(compressed);
        System.out.printf("length=%2d : %s%n", length, Arrays.toString(compressed));
    }
} finally {
    deflater.end();
}

// Result
// ↓
//length=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]

int deflate (byte[] output, int off, int len)

Compresses the input data and fills specified buffer with compressed data.

final var deflater = new Deflater();
try {
    final var input = "aaaaa".getBytes();

    // [97, 97, 97, 97, 97]
    System.out.println(Arrays.toString(input));

    deflater.setInput(input);
    deflater.finish();

    final var output1 = new byte[20];
    System.out.println(deflater.deflate(output1)); // 11

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output1));

    deflater.reset();
    deflater.setInput(input);
    deflater.finish();

    final var output2 = new byte[20];
    System.out.println(deflater.deflate(output2, 3, 15)); // 11

    // [0, 0, 0, 120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output2));
} finally {
    deflater.end();
}

int deflate (byte[] output, int off, int len, int flush)

Compresses the input data and fills the specified buffer with compressed data.

Please see deflate(byte[] output, int off, int len) for off, len parameters.

final var deflater = new Deflater();
try {
    deflater.setInput("aaaaa".getBytes());

    final var output1 = new byte[15];
    final var length1 = deflater.deflate(output1, 0, output1.length, Deflater.NO_FLUSH);
    System.out.println(length1); // 2

    // [120, -100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output1));

    deflater.finish();

    final var output2 = new byte[15];
    final var length2 = deflater.deflate(output2, 0, output2.length, Deflater.NO_FLUSH);
    System.out.println(length2); // 9

    // [75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output2));
} finally {
    deflater.end();
}
final var deflater = new Deflater();
try {
    deflater.setInput("aaaaa".getBytes());

    final var output1 = new byte[15];
    final var length1 = deflater.deflate(output1, 0, output1.length, Deflater.SYNC_FLUSH);
    System.out.println(length1); // 12

    // [120, -100, 74, 76, 4, 2, 0, 0, 0, 0, -1, -1, 0, 0, 0]
    System.out.println(Arrays.toString(output1));

    deflater.finish();

    final var output2 = new byte[15];
    final var length2 = deflater.deflate(output2, 0, output2.length, Deflater.SYNC_FLUSH);
    System.out.println(length2); // 6

    // [3, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output2));
} finally {
    deflater.end();
}
final var deflater = new Deflater();
try {
    deflater.setInput("aaaaa".getBytes());

    final var output1 = new byte[15];
    final var length1 = deflater.deflate(output1, 0, output1.length, Deflater.FULL_FLUSH);
    System.out.println(length1); // 12

    // [120, -100, 74, 76, 4, 2, 0, 0, 0, 0, -1, -1, 0, 0, 0]
    System.out.println(Arrays.toString(output1));

    deflater.finish();

    final var output2 = new byte[15];
    final var length2 = deflater.deflate(output2, 0, output2.length, Deflater.FULL_FLUSH);
    System.out.println(length2); // 6

    // [3, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output2));
} finally {
    deflater.end();
}

int deflate (ByteBuffer output)

Compresses the input data and fills specified buffer with compressed data.

final var deflater = new Deflater();
try {
    final var input = "aaaaa".getBytes();

    deflater.setInput(input);
    deflater.finish();

    final var output = ByteBuffer.allocate(15);
    final var length = deflater.deflate(output);
    System.out.println(length); // 11
    System.out.println(output); // java.nio.HeapByteBuffer[pos=11 lim=15 cap=15]

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output.array()));
} finally {
    deflater.end();
}

int deflate (ByteBuffer output, int flush)

Compresses the input data and fills the specified buffer with compressed data.

Please see :

void end ()

Closes the compressor and discards any unprocessed input.

Please see deflate(byte[] output).

void finish ()

When called, indicates that compression should end with the current contents of the input buffer.

Please see deflate(byte[] output).

boolean finished ()

Returns true if the end of the compressed data output stream has been reached.

Please see deflate(byte[] output).

int getAdler ()

Returns the ADLER-32 value of the uncompressed data.

final var deflater = new Deflater();
try {
    final var input = "aaaaa".getBytes();

    deflater.setInput(input);
    deflater.finish();

    System.out.println(deflater.getAdler()); // 1

    final var output = new byte[15];
    System.out.println(deflater.deflate(output)); // 11

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(output));

    System.out.println(deflater.getAdler()); // 95683046
} finally {
    deflater.end();
}

long getBytesRead ()

Returns the total number of uncompressed bytes input so far.

final var deflater = new Deflater();
try {
    final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();

    deflater.setInput(input);
    deflater.finish();

    while (!deflater.finished()) {
        final var compressed = new byte[10];
        final var length = deflater.deflate(compressed);
        final var total = deflater.getBytesRead();
        System.out.printf("length=%2d : total=%2d : %s%n",
                length, total, Arrays.toString(compressed));
    }
} finally {
    deflater.end();
}

// Result
// ↓
//length=10 : total=24 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=24 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]

long getBytesWritten ()

Returns the total number of compressed bytes output so far.

final var deflater = new Deflater();
try {
    final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();

    deflater.setInput(input);
    deflater.finish();

    while (!deflater.finished()) {
        final var compressed = new byte[10];
        final var length = deflater.deflate(compressed);
        final var total = deflater.getBytesWritten();
        System.out.printf("length=%2d : total=%2d : %s%n",
                length, total, Arrays.toString(compressed));
    }
} finally {
    deflater.end();
}

// Result
// ↓
//length=10 : total=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=20 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]

int getTotalIn ()

Returns the total number of uncompressed bytes input so far.

Please see also : getBytesRead()

final var deflater = new Deflater();
try {
    final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();

    deflater.setInput(input);
    deflater.finish();

    while (!deflater.finished()) {
        final var compressed = new byte[10];
        final var length = deflater.deflate(compressed);
        final var total = deflater.getTotalIn();
        System.out.printf("length=%2d : total=%2d : %s%n",
                length, total, Arrays.toString(compressed));
    }
} finally {
    deflater.end();
}

// Result
// ↓
//length=10 : total=24 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=24 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]

int getTotalOut ()

Returns the total number of compressed bytes output so far.

Please see also : getBytesWritten()

final var deflater = new Deflater();
try {
    final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();

    deflater.setInput(input);
    deflater.finish();

    while (!deflater.finished()) {
        final var compressed = new byte[10];
        final var length = deflater.deflate(compressed);
        final var total = deflater.getTotalOut();
        System.out.printf("length=%2d : total=%2d : %s%n",
                length, total, Arrays.toString(compressed));
    }
} finally {
    deflater.end();
}

// Result
// ↓
//length=10 : total=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=20 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]

boolean needsInput ()

Returns true if no data remains in the input buffer.

Please see setStrategy(int strategy).

void reset ()

Resets deflater so that a new set of input data can be processed.

Please see setStrategy(int strategy).

void setDictionary (byte[] dictionary)

Sets preset dictionary for compression.

final var input = "abcXYZabcXYZabcXYZ".getBytes();
final var dictionary = "abcXYZ".getBytes();

// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[15];
int compressedLength;
try {
    deflater.setDictionary(dictionary);
    deflater.setInput(input);
    deflater.finish();

    compressedLength = deflater.deflate(compressed);
    System.out.println(compressedLength); // 14

    // [120, -69, 7, -42, 2, 50, 75, -60, 32, 1, 62, -12, 6, -108, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

// --------
// Decompression
final var inflater = new Inflater();
try {
    inflater.setInput(compressed, 0, compressedLength);

    final var decompressed = new byte[20];
    System.out.println(inflater.inflate(decompressed)); // 0
    System.out.println(inflater.needsDictionary()); // true

    inflater.setDictionary(dictionary);

    final int decompressedLength = inflater.inflate(decompressed);
    System.out.println(decompressedLength); // 18

    // [97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 0, 0]
    System.out.println(Arrays.toString(decompressed));

    // abcXYZabcXYZabcXYZ
    System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
    inflater.end();
}

void setDictionary (byte[] dictionary, int off, int len)

Sets preset dictionary for compression.

final var input = "abcXYZabcXYZabcXYZ".getBytes();
final var dictionary = "abcXYZ".getBytes();

// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[20];
int compressedLength;
try {
    deflater.setDictionary(dictionary, 1, 4);
    deflater.setInput(input);
    deflater.finish();

    compressedLength = deflater.deflate(compressed);
    System.out.println(compressedLength); // 18

    // [120, -69, 3, -66, 1, 119, 75, 76, 2, 18, 81, -56, 36, 0, 62, -12, 6, -108, 0, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

// --------
// Decompression
final var inflater = new Inflater();
try {
    inflater.setInput(compressed, 0, compressedLength);

    final var decompressed = new byte[20];
    System.out.println(inflater.inflate(decompressed)); // 0
    System.out.println(inflater.needsDictionary()); // true

    inflater.setDictionary(dictionary, 1, 4);

    final int decompressedLength = inflater.inflate(decompressed);
    System.out.println(decompressedLength); // 18

    // [97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 0, 0]
    System.out.println(Arrays.toString(decompressed));

    // abcXYZabcXYZabcXYZ
    System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
    inflater.end();
}

void setDictionary (ByteBuffer dictionary)

Sets preset dictionary for compression.

final var input = "abcXYZabcXYZabcXYZ".getBytes();
final var dictionary = "abcXYZ".getBytes();

// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[15];
int compressedLength;
try {
    deflater.setDictionary(ByteBuffer.wrap(dictionary));
    deflater.setInput(input);
    deflater.finish();

    compressedLength = deflater.deflate(compressed);
    System.out.println(compressedLength); // 14

    // [120, -69, 7, -42, 2, 50, 75, -60, 32, 1, 62, -12, 6, -108, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

// --------
// Decompression
final var inflater = new Inflater();
try {
    inflater.setInput(compressed, 0, compressedLength);

    final var decompressed = new byte[20];
    System.out.println(inflater.inflate(decompressed)); // 0
    System.out.println(inflater.needsDictionary()); // true

    inflater.setDictionary(ByteBuffer.wrap(dictionary));

    final int decompressedLength = inflater.inflate(decompressed);
    System.out.println(decompressedLength); // 18

    // [97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 0, 0]
    System.out.println(Arrays.toString(decompressed));

    // abcXYZabcXYZabcXYZ
    System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
    inflater.end();
}

void setInput (byte[] input)

Sets input data for compression.

final var deflater = new Deflater();
try {
    deflater.setInput("aaaa".getBytes());

    final var output1 = new byte[5];
    System.out.println(deflater.deflate(output1)); // 2
    System.out.println(Arrays.toString(output1)); // [120, -100, 0, 0, 0]

    deflater.setInput("XXXX".getBytes());

    final var output2 = new byte[5];
    System.out.println(deflater.deflate(output2)); // 0
    System.out.println(Arrays.toString(output2)); // [0, 0, 0, 0, 0]

    deflater.finish();

    final var output3 = new byte[15];
    System.out.println(deflater.deflate(output3)); // 12

    // [75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27, 0, 0, 0]
    System.out.println(Arrays.toString(output3));
} finally {
    deflater.end();
}

void setInput (byte[] input, int off, int len)

Sets input data for compression.

final var deflater = new Deflater();
try {
    final var input = "abcdXYZ".getBytes();

    // [97, 98, 99, 100, 88, 89, 90]
    System.out.println(Arrays.toString(input));

    deflater.setInput(input, 2, 4);
    deflater.finish();

    final var compressed = new byte[15];
    System.out.println(deflater.deflate(compressed)); // 12

    // [120, -100, 75, 78, -119, -120, 4, 0, 3, -59, 1, 121, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));

    // Decompression
    try (final var ios = new InflaterInputStream(new ByteArrayInputStream(compressed))) {
        final var decompressed = ios.readAllBytes();
        System.out.println(Arrays.toString(decompressed)); // [99, 100, 88, 89]
        System.out.println(new String(decompressed)); // cdXY
    }
} finally {
    deflater.end();
}

void setInput (ByteBuffer input)

Sets input data for compression.

final var deflater = new Deflater();
try {
    final var input = ByteBuffer.wrap("aaaaa".getBytes());
    System.out.println(input); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
    System.out.println(Arrays.toString(input.array())); // [97, 97, 97, 97, 97]

    deflater.setInput(input);
    deflater.finish();

    final var compressed = new byte[15];
    System.out.println(deflater.deflate(compressed)); // 11

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

void setLevel (int level)

Sets the compression level to the specified value.

Please see also : Deflater(int level)

final var deflater = new Deflater();
try {
    final var input = "aaaaa".getBytes();

    // [97, 97, 97, 97, 97]
    System.out.println(Arrays.toString(input));

    deflater.setLevel(Deflater.NO_COMPRESSION);
    deflater.setInput(input);
    deflater.finish();

    final var compressed = new byte[20];
    System.out.println(deflater.deflate(compressed)); // 0
    System.out.println(deflater.needsInput()); // false
    System.out.println(deflater.deflate(compressed)); // 16
    System.out.println(deflater.needsInput()); // true

    // [120, 1, 1, 5, 0, -6, -1, 97, 97, 97, 97, 97, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));
} finally {
    deflater.end();
}

void setStrategy (int strategy)

Sets the compression strategy to the specified value.

final var deflater = new Deflater();
try {
    final var input = "aaaaa".getBytes();

    // [97, 97, 97, 97, 97]
    System.out.println(Arrays.toString(input));

    deflater.setStrategy(Deflater.FILTERED);
    deflater.setInput(input);
    deflater.finish();

    final var filtered = new byte[15];
    System.out.println(deflater.deflate(filtered)); // 0
    System.out.println(deflater.needsInput()); // false
    System.out.println(deflater.deflate(filtered)); // 13
    System.out.println(deflater.needsInput()); // true

    // [120, -100, 75, 76, 76, 76, 76, 4, 0, 5, -76, 1, -26, 0, 0]
    System.out.println(Arrays.toString(filtered));

    deflater.reset();
    deflater.setStrategy(Deflater.HUFFMAN_ONLY);
    deflater.setInput(input);
    deflater.finish();

    final var huffman = new byte[15];
    System.out.println(deflater.deflate(huffman)); // 0
    System.out.println(deflater.needsInput()); // false
    System.out.println(deflater.deflate(huffman)); // 13
    System.out.println(deflater.needsInput()); // true

    // [120, 1, 75, 76, 76, 76, 76, 4, 0, 5, -76, 1, -26, 0, 0]
    System.out.println(Arrays.toString(huffman));

    deflater.reset();
    deflater.setStrategy(Deflater.DEFAULT_STRATEGY);
    deflater.setInput(input);
    deflater.finish();

    final var def = new byte[15];
    System.out.println(deflater.deflate(def)); // 0
    System.out.println(deflater.needsInput()); // false
    System.out.println(deflater.deflate(def)); // 11
    System.out.println(deflater.needsInput()); // true

    // [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
    System.out.println(Arrays.toString(def));
} finally {
    deflater.end();
}

Related posts

To top of page