I'm trying to read in the whole file at once. If this code ever fails, my whole project will fail. I'm using the following:

byte[] theFile = new byte[input2.available()];
then, input2.read(theFile);

The API says something about blocking. What exactly is blocking, when will it happen, and how will it affect the fact that I want the whole file to be read in at once?

Recommended Answers

All 8 Replies

Use the power of the Internet to find it out....

Use the

read(byte[], offset, length)

That method returns how many bytes were successfully read, or -1 at EOF.

Use that return value to modify the offset and the length and call read again.

Doing this in a while loop (i.e. != -1) will ensure your success (in the absence of an IO error, of course).

Try it and if you have a problem post that code.

Use the power of the Internet to find it out....

That's exactly what I'm doing by posting on Daniweb. Not out of laziness, but I was short on time and I was fairly sure I would have follow up questions, so I chose to post here. Since I probably answer more questions than I ask, I'm sure you guys will forgive me. :) Its a shame that when I mark this thread solved, you will now get credit for it, although you didn't answer the question or offer an alternate solution. Either way, thanks to Masijade.... an alternate solution is just as good.

The API says something about blocking. What exactly is blocking, when will it happen, and how will it affect the fact that I want the whole file to be read in at once?

Blocking in that context means that code execution will halt (or wait) until one of the conditions listed there is met.

This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

So essentially, as long as an IO error does not occur and the end of the stream has not been reached, it will wait for data to be available to read, rather than immediately returning some empty result. It "blocks" your code from continuing until it's satisfied the return condition.

Ok, I kind of understand that. But it doesn't guarantee that it will read to the end of the stream, so isn't there a possibility that input will be available, but not all of the input? If so, how would that occur? Is there some buffer to be considered or something?

It all depends on which read() method you use. The exact behavior is detailed in the API doc of those methods.

That's exactly what I'm doing by posting on Daniweb. Not out of laziness, but I was short on time and I was fairly sure I would have follow up questions, so I chose to post here. Since I probably answer more questions than I ask, I'm sure you guys will forgive me. :) Its a shame that when I mark this thread solved, you will now get credit for it, although you didn't answer the question or offer an alternate solution. Either way, thanks to Masijade.... an alternate solution is just as good.

I did not mean to hurt you at all although you took it in a negative way, what I just meant was you could have found the solution googling it rather quickly than on this forum also it would have been in detail which I thought you would be wanting to know since your question was so specific and all this would just add up to your knowledge about the question domain thats it.
As far as the credit goes you can may as well request the moderator or whosoever has the power to, to not to add this thread to my solved threads (or subtract the count, if it can be done so) since as matter of fact I too don't like to take credit for the stuff I haven't actually contributed to.

I'm not upset, there just wasn't anything to be gained from your previous post. And its certainly not serious enough to waste a mod's time with removing one from your solved threads count, which they probably cannot do anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.