Java : InflaterInputStream (ZIP) with Examples
InflaterInputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most InflaterInputStream methods.
Summary
// --------
// 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
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected Inflater inf
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int len
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in FilterInputStream
Constructors
InflaterInputStream (InputStream in)
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)
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)
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 ()
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 ()
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 ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
void mark (int readlimit)
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 ()
Please see mark(int readlimit).
int read ()
Please see available().
int read (byte[] b, int off, int len)
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 ()
Please see mark(int readlimit).
long skip (long n)
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
Methods declared in InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
Please see the link below.
Related posts
- API Examples