#Conceptual question about streams

1 messages · Page 1 of 1 (latest)

vale wedgeBOT
#

<@&987246399047479336> please have a look, thanks.

vale wedgeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

frail raptor
#

When I have a line such as new ObjectOutputStream(new ZipOutputStream(new FileOutputStream("some file")););.

I’m basically creating, as far as I understand it, a stream towards some certain file, which can already exist or it will be created. (With new FileOutputStream("some file")) Afterwards, and correct me if I’m wrong, I’m applying a certain number of filters on this stream towards the file, by having access to certain methods which let me use those filters, for example, by having the capacity to write/translate objects in a stream of byte ( With new ObjectOutputStream) and this stream of byte, I assume, is compressed to a ZIP format by using (new ZipOutputStream) before being sent off officially towards the file. (Though new FileOutputStream("some file"));) I'm basically preparing the "stream" to a certain format before officially sending it off, instead of directly dealing with the bytes.
Why is it, in this case, that when I need to close the stream, if my understanding is correct (it might not be), I always have to close the last “filter” (ObjectOutputStream in this case) which I use and not the actual stream towards the file (FileOutputStream) through which we send our data?

#

Hopefully my question makes sense

shell pasture
#

Well you don't strictly need to close it I think, but you do need to flush the stream (which is what closing does as well). You're basically telling the filters as you call them to write whatever is buffered to disk

#

Just checked, I am incorrect regarding ZipOutputStream, that does need to be closed

#

Because it's a signal to the deflate stream that it has all the data, which means it'll write the last chunk of compressed data

#

Rather than waiting for more to compress it with

frail raptor
#

Intuitively, it can make sense to close the outermost method, because we want to flush eveything towards the file while writing, but when we read should we not close the input towards the file first, so starting from innermost, and towards outermost?

#

Is this what is actually happening?

shell pasture
#

No for reading you generally read until you get end of file

#

And outermost layers will generally block on the innermost stream