EDIT: For a general stream, you just keep reading until you've read everything you want to, basically. For an implementation of DataInput (such as DataInputStream ) you should use readFully as suggested by Peter Lawrey. Consider the rest of this answer to be relevant for the general case where you just have an InputStream.
EDIT: For a general stream, you just keep reading until you've read everything you want to, basically. For an implementation of DataInput (such as DataInputStream) you should use readFully, as suggested by Peter Lawrey. Consider the rest of this answer to be relevant for the general case where you just have an InputStream.It's entirely reasonable for an InputStream of any type to give you less data than you asked for, even if more is on its way.
You should always code for this possibility - with the possible exception of ByteArrayInputStream. (That can still return less data than was requested, of course, if there's less data left than you asked for.) Here's the sort of loop I'm talking about: byte data = new bytemessageSize; int totalRead = 0; while (totalRead.
ByteArrayInputStream will still read less than requested if you reach its end. – Stephen Denne Jan 12 at 21:06 Thanks, makes sense. – barab157 Jan 12 at 21:10 @Stephen: That's true, but if you absolutely know that there's more data there, then I'd be very surprised to see it return less data.
I'll edit to clarify though. – Jon Skeet Jan 12 at 21:12 Nice explanation of what was happening. However, I think Peter Lawrey's suggestion to use readFully is cleaner than using a while loop to read the full amount of data.
– Dunes Jan 12 at 23:30 @Dunes: In the case of DataInputStream, you're absolutely right :) I'd missed that. Useful to have the loop version for non-DataInput. Will edit.
– Jon Skeet Jan 12 at 23:32.
You can use DataInputStream this way. Byte bytes = new bytelen; dis. ReadFully(bytes); This will either return with all the data read or throw an IOException.
Read returns each time with the bits that were available at that time and -1 when done, you are typically supposed to do while (true) { int numRead = dis. Read(buffer, 0, len); if (numRead == -1) break; total. Append(buffer, numRead); }.
This is just wrong, it does not decrement len. – dacwe Jan 12 at 21:19 yeah, len can be constant here e.g. 1024 – Yuval Rimar Jan 12 at 21:39.
I would expect the behavior of read to block until the stream is closed or EOF is reached. Then you need to check the Javadocs. The contract of read() is that it will read at least one byte, blocking if necessary until it has done so, or until EOS or an exception occurs.
There is nothing in the specification that says it will read the entire length you requested. That's why it returns a length.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.