user3283519
user3283519

Reputation: 15

getting negative length value of bytearray

soc = new Socket(ip, port1);
    while (true)
    {
        DataInputStream dis = new DataInputStream(soc.getInputStream());
        int len = dis.readInt();
        Log.d("===============" + len, "-------------------" + len);
        byte data[] = null;
        data = new byte[len];
        dis.readFully(data);

        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, len);
        ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageBitmap(bmp);
    }

This is my android code which takes the byte array from server and it display in Image.I am getting negative value of length of byte array and so app gets stopped. The above code working properly in PC. and also tell me whether I am doing right method for displaying image to screen in android.

Logcat field:

03-03 12:50:27.941: E/AndroidRuntime(7283): FATAL EXCEPTION: Thread-205
03-03 12:50:27.941: E/AndroidRuntime(7283): java.lang.NegativeArraySizeException: -1393754107
03-03 12:50:27.941: E/AndroidRuntime(7283):     at com.example.vnc.Screen$1.run(Screen.java:47)
03-03 12:50:27.941: E/AndroidRuntime(7283):     at java.lang.Thread.run(Thread.java:856)

Server Side:

        while(continueLoop){

        try{
        BufferedImage image = robot.createScreenCapture(rectangle);


        byte[] imageInByte;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image,"jpg", baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        baos.close();
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeInt(imageInByte.length);
        System.out.println(imageInByte.length);
        dos.write(imageInByte);
        Thread.sleep(1000);
        dos.flush();
        }
        catch(Exception e)
        {}
                   try{
            Thread.sleep(100);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

Upvotes: 1

Views: 1771

Answers (2)

Stephen C
Stephen C

Reputation: 719336

This is my android code which takes the byte array from server and it display in Image.I am getting negative value of length of byte array and so app gets stopped.

What your code is doing is reading an int which it is interpreting an array size. And sometimes the value it reading is negative ... which is nonsense.

I suspect that the real problem is that the way that you are reading the data does not match the way that you wrote it. If you showed us the code that does the writing (or its specification) we would have a better chance of telling you what the actual problem is, and recommending a solution that will actually work.


UPDATE

I think I get it. I think that this is due to some internal buffering in the DataOutputStream ... combined with the fact that you are opening a new DataOutputStream each time you go round the loop. What happens is this:

  1. One time around the loop, you open the DataInputStream and read from it. This cause all currently available bytes to be buffered.
  2. Then you read the size and the bytes, leaving the first few bytes of the next "message" unread in the buffer.
  3. But next time round the loop you open another DataInputStream. This discards the unread bytes, and causes the reader to read a "length" from the wrong place in the logical stream; i.e. some random point in an image. The chances are > 50% that this will look like a negative or large positive value ... leading to the exception.

Move this line to before the loop:

 DataInputStream dis = new DataInputStream(soc.getInputStream());

You should probably do the same thing on the server side too, though the fact that you are explicitly flushing dos should save you there.

Upvotes: 1

Lucian Novac
Lucian Novac

Reputation: 1265

use Guava's UnsignedBytes utilities to view Java bytes as unsigned http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/UnsignedBytes.html

Upvotes: 0

Related Questions