6

In the following scenario

ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
// Do stuff with it

Why is it always necessary to flush the buffer after initial creation?
I see this all the time and I don't really understand what has to be flushed. I kind of expect newly created variables to be empty unless otherwise is specified.

Kind of like buying a trash-can and finding a tiny pile of trash inside that came with it.

Touchstone
  • 5,575
  • 7
  • 41
  • 48
krystah
  • 3,587
  • 2
  • 25
  • 43
  • I don't think it is really necessary. What says the javadoc? – robermann Mar 03 '14 at 11:25
  • 4
    I don't see this all the time... or ever, in fact. Where are you encountering it? – Marko Topolnik Mar 03 '14 at 11:25
  • I never do that. And never faced problem too. – codingenious Mar 03 '14 at 11:28
  • The javadocs doesn't specify to do so, but I've had multiple professors and read quite a few tutorials suggesting that it's good practice, I just don't like doing stuff "just because some smart guy said it". – krystah Mar 03 '14 at 11:35
  • 1
    Can you share links to these tutorials? – Pshemo Mar 03 '14 at 11:36
  • Is it possible you are seeing these flushes being called with server sockets. On these sockets you are creating the object output stream after sending prior data through the socket. I could see an instance where you write some data to the socket then want to write some more but it could take some time. In that case you would want to flush it prior to doing the heavy lifting. – ug_ Mar 03 '14 at 11:42
  • 2
    It's been a while since I read those tutorials, but I dug up a couple here: https://blogs.oracle.com/muraliveligeti/entry/j2ee_connecting_to_https_site http://www.binarytides.com/java-socket-programming-tutorial/ – krystah Mar 03 '14 at 11:51
  • I might be better off asking the professors why this is rather than looking up random tutorials in a hurry though. – krystah Mar 03 '14 at 11:53

3 Answers3

2

In over 15 years of writing Java on a professional level I've never once encountered a need to flush a stream before writing to it.
The flush operation would do nothing at all, as there's nothing to flush.
You want to flush the stream before closing it, though the close operation should do that for you it is often considered best practice to do it explicitly (and I have encountered situations where that did make a difference, where apparently the close operation did not actually do a flush first.
Maybe you are confused with that?

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • I am not confusing flushing after initial creation with flushing prior to closing streams. I have been led to believe that after retrieving the OutputStream-object, some stuff resides in the buffer from the very start. I just wanted to know *what* that is and why it is even there. – krystah Mar 03 '14 at 12:29
1

When you write data out to a stream, some amount of buffering will occur, and you never know for sure exactly when the last of the data will actually be sent. You might perform many rite operations on a stream before closing it, and invoking the flush()method guarantees that the last of the data you thought you had already written actually gets out to the file. Whenever you're done using a file, either reading it or writing to it, you should invoke the close()method. When you are doing file I/O you're using expensive and limited operating system resources, and so when you're done, invoking close()will free up those resources.

Saurabh Jain
  • 1,600
  • 1
  • 20
  • 30
  • 1
    I'm not sure this explains why it's necessary to flush the output stream *before* you perform any write operations. – Anthony Grist Mar 03 '14 at 11:30
  • Yes after writing to the stream but what about after initial creation? I dont see the same situation occurring prior to writing anything. – ug_ Mar 03 '14 at 11:30
  • 1
    True, but seems to have nothing to do with question. OP is asking why stream needs to be flushed right after it is created (where there shouldn't be any data in). – Pshemo Mar 03 '14 at 11:31
  • When reading a file, no flushing is required, so you won't even find a flush()method in a Reader kind of class.For all your output files, you might discover that the buffers don’t get flushed, so the file will be incomplete and you may not see the output. – Saurabh Jain Mar 03 '14 at 11:41
0

This is needed when using either ObjectInputStream and ObjectOutputStream, because they send a header over the stream before the first write is called. The call to flush() will send that header to the remote side.

According to the spec, the header exists of the following contents:

magic version

If the header doesn't arrive at the moment a ObjectInputStream is build, this call will hang until it received the header bytes.

This means that if the protocol in question is written with ObjectStreams, it should flush after creating a ObjectOutputStream.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79