0

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. It works very fine, when i tested through iOS simulator. Because, If I test in simulator, it sends the image upto 46,577 bytes. It reads all very quickly and properly. If I test the same code sending image from an iPhone device, its also sending around "45, 301 bytes", but socket code is able to read only some "21, 720 bytes", so only half of the image is coming, (or) sometimes it reads very less around "4,000 bytes" only.

I couldn't understand why it is not able to read the same size data coming from device only? Could someone please guide me on this to solve?

InputStream input = socket.getInputStream();

            byte[] data = new byte[0];
            byte[] buffer = new byte[1024];             

            try {
                do {
                bytesRead = input.read(buffer);
                System.out.println("Reading..bytesRead: " + bytesRead);

                // construct an array large enough to hold the data we currently have
                byte[] newData = new byte[data.length + bytesRead];
                // copy data that was previously read into newData
                System.arraycopy(data, 0, newData, 0, data.length);
                // append new data from buffer into newData
                System.arraycopy(buffer, 0, newData, data.length, bytesRead);
                // set data equal to newData in prep for next block of data
                data = newData;

                } while (input.available() != 0);
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

            System.out.println("data: " + data.length);
8
  • Can you try reading a byte at a time with input.read() and using input.read() != -1 as the while loop's terminating condition? Because InputStream.available() returns the no. of bytes that can be read without blocking; it doesn't necessarily indicate that the stream has ended. Something along the lines of: while((next = input.read()) != -1) { byteArrayOutputStream.write(next); } byte[] data = byteArrayOutputStream.toByteArray(); Commented Feb 12, 2014 at 4:52
  • I couldn't do this. Can you please give the full sample please? Commented Feb 12, 2014 at 5:11
  • I tried like you suggested. byte[] data = new byte[16384]; ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); while ((nRead = input.read(data, 0, data.length)) != -1) { byteBuffer.write(data, 0, nRead); byte[] dataOut = byteBuffer.toByteArray(); Client keeps looping of sending data without stop and after some time, socket got read all and crashed the client app. Commented Feb 12, 2014 at 5:21
  • InputStream in = null; try { int next; ByteArrayOutputStream out = new ByteArrayOutputStream(); while((next = in.read()) != -1) { out.write(next); } byte[] data = out.toByteArray(); } catch(IOException e) { //handle error } Commented Feb 12, 2014 at 5:22
  • Let me try your code as well. Commented Feb 12, 2014 at 5:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Should i have to use ByteArrayOutputStream or FileOutputStream? You added code with ByteArrayOutputStream, but you mentioned write to FileOutputStream? So, What should i use ByteArrayOutputStream or FileOutputStream?
Issue is, It's not reading completely all bytes and coming out of this loop. "While" loop gets stuck up just before completing the size of bytes. For ex: If client sends 50 K bytes image data, then this socket "While" reads upto some around 49 K bytes, after that it is not quitting the this loop, stuck up in the loop itself.
Found a strange thing, when its reading in the 'While' loop in socket, if i exit the iOS client app, then the socket gets the full image retrieved abruptly. So, this socket 'While' loop somehow hangs it.
@Stella So your client wasn't closing the socket. The while() loop will block until you do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.