Java : InflaterInputStream (ZIP) with Examples

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


Summary

This class implements a stream filter for uncompressing data in the "deflate" compression format. It is also used as the basis for other decompression filters, such as GZIPInputStream.

Class diagram

// --------
// Compression
final var buf = "aaaaaaaaaa".getBytes();
try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    final var compressed = dis.readAllBytes();

    // [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
    System.out.println(Arrays.toString(compressed));

    // --------
    // Decompression
    try (final var iis = new InflaterInputStream(new ByteArrayInputStream(compressed))) {
        final var uncompressed = iis.readAllBytes();

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

        // aaaaaaaaaa
        System.out.println(new String(uncompressed));
    }
}

Helper method for code examples

Code examples on this page use the helper method below.

// Compress a string.
public byte[] deflate(String str) throws IOException {
    final var out = new ByteArrayOutputStream();
    try (final var dos = new DeflaterOutputStream(out)) {
        dos.write(str.getBytes());
    }

    return out.toByteArray();
}

Fields

protected byte[] buf

Input buffer for decompression.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected Inflater inf

Decompressor for this stream.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int len

Length of input buffer.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Fields declared in FilterInputStream

in

Please see the link below.

Constructors

InflaterInputStream (InputStream in)

Creates a new input stream with a default decompressor and buffer size.

The deflate method is here.

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

try (final var iis = new InflaterInputStream(new ByteArrayInputStream(bytes))) {
    final var uncompressed = iis.readAllBytes();

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

    // aaaaaaaaaa
    System.out.println(new String(uncompressed));
}

InflaterInputStream (InputStream in, Inflater inf)

Creates a new input stream with the specified decompressor and a default buffer size.

The deflate method is here.

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

try (final var iis = new InflaterInputStream(
        new ByteArrayInputStream(bytes), new Inflater())) {
    final var uncompressed = iis.readAllBytes();

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

    // aaaaaaaaaa
    System.out.println(new String(uncompressed));
}

InflaterInputStream (InputStream in, Inflater inf, int size)

Creates a new input stream with the specified decompressor and buffer size.

Please see InflaterInputStream(InputStream in, Inflater inf) for in, inf parameters.

class MyInputStream extends InflaterInputStream {
    public MyInputStream(InputStream in) {
        super(in, new Inflater());
    }

    public MyInputStream(InputStream in, int size) {
        super(in, new Inflater(), size);
    }

    int getSize() {
        return buf.length;
    }
}

try (final var is = new MyInputStream(new ByteArrayInputStream(new byte[0]))) {
    System.out.println(is.getSize()); // 512
}

try (final var is = new MyInputStream(new ByteArrayInputStream(new byte[0]), 1024)) {
    System.out.println(is.getSize()); // 1024
}

Methods

int available ()

Returns 0 after EOF has been reached, otherwise always return 1.

The deflate method is here.

final var str = "abc";
System.out.println(Arrays.toString(str.getBytes())); // [97, 98, 99]

final var bytes = deflate(str);

// [120, -100, 75, 76, 74, 6, 0, 2, 77, 1, 39]
System.out.println(Arrays.toString(bytes));

try (final var iis = new InflaterInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(iis.available()); // 1

    System.out.println(iis.read()); // 97
    System.out.println(iis.available()); // 1

    System.out.println(iis.read()); // 98
    System.out.println(iis.available()); // 1

    System.out.println(iis.read()); // 99
    System.out.println(iis.available()); // 1

    System.out.println(iis.read()); // -1
    System.out.println(iis.available()); // 0
}

void close ()

Closes this input stream and releases any system resources associated with the stream.

The deflate method is here.

final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
Files.write(file, bytes);

try (final var iis = new InflaterInputStream(Files.newInputStream(file))) {
    final var uncompressed = iis.readAllBytes();

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

    // aaaaaaaaaa
    System.out.println(new String(uncompressed));
}

An example without try-with-resources.

final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
Files.write(file, bytes);

final var iis = new InflaterInputStream(Files.newInputStream(file));
try {
    final var uncompressed = iis.readAllBytes();

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

    // aaaaaaaaaa
    System.out.println(new String(uncompressed));
} finally {
    iis.close();
}

protected void fill ()

Fills input buffer with more data to decompress.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

void mark (int readlimit)

Marks the current position in this input stream.

The deflate method is here.

final var bytes = deflate("aaaaaaaaaa");
System.out.println("bytes : " + Arrays.toString(bytes));

try (final var iis = new InflaterInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println("markSupported : " + iis.markSupported());

    iis.mark(0);
    iis.reset();
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//bytes : [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
//markSupported : false
//IOException! : mark/reset not supported

boolean markSupported ()

Tests if this input stream supports the mark and reset methods.

Please see mark(int readlimit).

int read ()

Reads a byte of uncompressed data.

Please see available().

int read (byte[] b, int off, int len)

Reads uncompressed data into an array of bytes.

The deflate method is here.

final var str = "abcdef";
System.out.println(Arrays.toString(str.getBytes())); // [97, 98, 99, 100, 101, 102]

final var bytes = deflate(str);

// [120, -100, 75, 76, 74, 78, 73, 77, 3, 0, 8, 30, 2, 86]
System.out.println(Arrays.toString(bytes));

try (final var iis = new InflaterInputStream(new ByteArrayInputStream(bytes))) {
    final var b1 = new byte[5];
    System.out.println(iis.read(b1, 0, 3)); // 3
    System.out.println(Arrays.toString(b1)); // [97, 98, 99, 0, 0]

    final var b2 = new byte[5];
    System.out.println(iis.read(b2, 1, 2)); // 2
    System.out.println(Arrays.toString(b2)); // [0, 100, 101, 0, 0]

    final var b3 = new byte[5];
    System.out.println(iis.read(b3, 0, 5)); // 1
    System.out.println(Arrays.toString(b3)); // [102, 0, 0, 0, 0]
}

void reset ()

Repositions this stream to the position at the time the mark method was last called on this input stream.

Please see mark(int readlimit).

long skip (long n)

Skips specified number of bytes of uncompressed data.

The deflate method is here.

final var str = "abcdef";
System.out.println(Arrays.toString(str.getBytes())); // [97, 98, 99, 100, 101, 102]

final var bytes = deflate(str);

// [120, -100, 75, 76, 74, 78, 73, 77, 3, 0, 8, 30, 2, 86]
System.out.println(Arrays.toString(bytes));

try (final var iis = new InflaterInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(iis.skip(3)); // 3

    final var uncompressed = iis.readAllBytes();

    // [100, 101, 102]
    System.out.println(Arrays.toString(uncompressed));

    // def
    System.out.println(new String(uncompressed));
}

Methods declared in FilterInputStream

read

Please see the link below.

Methods declared in InputStream

nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo

Please see the link below.


Related posts

To top of page