views:

171

answers:

3

Hi all,

Hopefully this is a quick and easy question about BufferedOutputStreams and DataOutputStreams in java.

When I write bytes to the output stream like

myBufferedOutputStream.write(array);
myDataOutputStream.write(array);

Do these methods write them instantly to the stream and return or do they ever block?

I did not see anything in the core java docs, but perhaps my question doesn't make sense because writes never block?

Someone please set me straight.

Thanks, jbu

A: 

I believe it depends on the implementation. If you're using something like BufferedOutputStream, a call to write() will likely not "block" since the class provides buffering for performance.

However, if using FileOutputStream, the write call may block depending on how busy/available the I/O resources are on the system, since a call to write() may actually trigger an I/O operation at that time, which takes time to complete.

Peter
+1  A: 

All of the read and write methods in java.io.* have the potential to block. None support asynchronous I/O. For reading, it must be implemented manually using .available() or a similar mechanism. For writing, well, you're on your own.

lavinio
A: 

The API in the package java.io.* is potential to block. However, there´s a special API called Java NIO (New I/O or Non-blocking I/O) that you should use for aynchronous I/O.

Look at the package java.nio.* You can find some examples here: http://en.wikipedia.org/wiki/New_I/O

Dani Cricco