The flush method of OutputStream does nothing.

I just don't get this, maybe I'm just plain dumb,

but could someone explain me this.

If I have a DataOuputStream, and I call its

flush() method, what really happens?

The API doc of DataOutputStream says

Flushes this data output stream. This forces

any buffered output bytes to be written out to the stream.

The flush method of DataOuputStream calls the

flush method of its underlying output stream.

And the API doc of OutputStream says

The flush method of OutputStream does nothing.

What does this mean? Nothing happens? The

DataOutputStream just calls OutputStreams

flush(), which does nothing?

Or does it mean that the DataOutputStream.flush()

first forces the bytes to be written to stream and then

calls the OutputStreams flush(), which actually does nothing?

kari-matti

[912 byte] By [kari-mattia] at [2007-10-3 1:04:31]
# 1

By default an OutputStream doesn't do any buffering, thus it's flush method doesn't need to do anything. If you've got a BufferedOutputStream, for example, then indeed some buffering will be done, thus calling flush() will have an effect.

Basically what I want to say is: The statement "The flush method of OutputStream does nothing" just tells you the behaviour of the default implementation, but classes extending OutputStream are free to change that behaviour, as long as it matches the contract defined in the API doc (and should do so, if its usefull).

Message was edited by:

JoachimSauer

JoachimSauera at 2007-7-14 18:00:52 > top of Java-index,Java Essentials,Java Programming...
# 2

> The flush method of DataOuputStream calls the

> flush method of its underlying output stream.

>

> And the API doc of OutputStream says

...

> What does this mean? Nothing happens?

>

> The DataOutputStream just calls OutputStreams

> flush(), which does nothing?

Do you know what an "abstract class" is? :) Who said that the "underlying stream" is a pure abstract OutputStream?

CeciNEstPasUnProgrammeura at 2007-7-14 18:00:52 > top of Java-index,Java Essentials,Java Programming...
# 3
The flush method of DataOuputStream calls theflush method of its underlying output stream.The underlying output stream is not always a plain OutputStream. It might be some other implementation of OutputStream that does do something.
jesperdja at 2007-7-14 18:00:52 > top of Java-index,Java Essentials,Java Programming...
# 4

Hello Kari.,

This is Samba;

Nice to talk to you again.

And coming to the question... I didn't understand what you trying to know.

One thing is clear.

What BufferedOutputStream does when flush() method is callled is it forcefully pushes out any of the data that is in it's buffer to the output device(eg. file, console, frame,webpage,etc), even before the buffer is filled, which otherwise waits till the buffer is filled.

But DataOutputStream doesn't do any buffering , so there is no questioning of flushing out the data. May be To keep all the Stream Classes Consistentthe SUN has created a dummy method in DataOutputStream.

Tell me if you know any thing more,

Samba.

Message was edited by:

MasterJ

MasterJa at 2007-7-14 18:00:52 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks to all for their replys, I believe I'm less

dumb now :)

So, basically the class that extends the OutputStream

class has to implement the abstract methods of OutputStream

and do its own flushing if necessary, right?

So incase of DataOutputStream, and other classes that

doesn't have buffering, the flush() method only calls the

super class' flush() method?

And to Samba: Nice to hear from you, I wish all good for you.

kari-matti

kari-mattia at 2007-7-14 18:00:52 > top of Java-index,Java Essentials,Java Programming...