I am having problem reading image bytes in java socket. My iOS client is sending an image here, and it needs to read the total bytes and store that as image on the server end. <
You're misusing available()
. It is not valid as a test for end of stream. See the Javadoc.
You don't need all that array copying either. Try this:
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
byte[] data = out.toByteArray();
If you're storing the image at the receiving end you should just write direct to a FileOutputStream
instead of the ByteArrayOutputStream
above, and forget about data
altogether.