It's protected
: It is intended for anything subclassing that stream, and you're not subclassing it, so as far as you are concerned, it is an implementation detail you cannot include in your reasoning and which isn't meant for you to invoke.
Unless, of course, you subclass it.
Which you could - it's sort of a toolkit for building LZ-based compression streams on top of. That's why both GZipOutputStream and ZipOutputStream extend it: Those are different containers that more or less use the same compression technology. And they do invoke that deflate
. Unless you're developing your own LZ-based compression system or implementing a reader for an existing, non-zip, non-gz, non-deflater based compression format, this is not meant for you.
These kinds of outputstreams are called 'filterstreams': They do not themselves represent any resource, they wrap around one. They can wrap around any OutputStream
(or any InputStream
, the concept works on 'both sides' so to speak), and modify bytes in transit.
var out = new DeflaterOutputStream(whatever)
creates a new deflater stream that will compress any data you send to it (via out.write(stuff)
), and it will in turn take the compressed data and send it on to whatever. It does the job of:
- take bytes (as per
out.write
), buffer as much as is needed to do the job:
- ... of compressing this data.
- Then process the compressed data, as it becomes compressed, by sending it to the wrapped outputstream (
whatever
, in this example), by calling its write
method.
The basic usage is:
- Create a resource, such as
Files.newOutputStream
or someSocket.getOutputStream
or httpServletResponse.getOutputStream()
or System.out
or anything else that produces a stream - it's a abstract concept for a reason: To make things flexible.
- Wrap that resource into a
DeflaterOutputStream
- Write all your data to the deflateroutputstream. Forget about the original - you made it so you can pass it to DeflaterOutputStream, and that's where your interaction with the underlying stream ends.
- Close the deflaterstream (which will end up closing the underlying stream as well).