Use DataInputStream.readFully(), but in this case what you really need is DataInputStream.readInt().
How do I read exactly 4 bytes from an InputStream?
Question
I'm sending JPEGs as byte arrays over a Socket, the 4 first bytes is the length of the JPEG. I have to read these in order to figure out how many bytes i should read to get the actual image. I can't seem to figure out how to do this.
Just reading like this is not guaranteed to give me my 4 bytes:
byte[] JPEGlength = new byte[4];
inputStream.read(lengthBuffer);
I have been thinking over this a lot and I cant seem to figure out how to do this. Help would be very appreciated!
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow