io - available and read: can we actually be nonblocking while using blocking Java I/O? -


Based on this, I want to ask the following. Hold blocked I / O and I have a piece of code like this:

  byte [] data = new byte [10]; Some inputstream.red (data)  

This code is going to block the snippet in the read call, unless there are some bytes to read it. I am aware that read can actually read fewer bytes and the number of bytes read is being returned by the method read .

My question is this. Let's say I have:

  byte [] data = new byte [10]; If (some InputStream.available ()> = 10) {someInputStream.read (data); // ***}  

What is the guarantee of not blocking the line ***? Again, I know that reading this can read less than 10 bytes.

This is not guaranteed to block

InputStream , looking at the available () method:

gives an estimate for these input streams to prevent these input streams from being blocked by the next call (Or left) of the number of bytes can be read from the next orientation can be the same thread or the second thread This single bytes will not be left to read or drop, Can read or leave fewer bytes.

(emphasis my.)

So this will not be a block, but (as you say) you will not get full 10 bytes.

Note that this is assuming a single thread, if there are many threads, then another formula is available by available () and read () .

will be read from the stream between.

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -