Link to home
Start Free TrialLog in
Avatar of UrosVidojevic
UrosVidojevicFlag for Serbia

asked on

ObjectOutputStream.writeObject() behaves oddly

The following code fragment:


        byte[] data = new byte[BUFFER_SIZE];
        byte[] end = null;
...
...
            while ((read = in.read(data, 0, BUFFER_SIZE)) != -1) {
                if (read == BUFFER_SIZE) {
                    out.writeObject(data);
                } else {
                    end = new byte[read];
                    System.arraycopy(data, 0, end, 0, read);
                    out.writeObject(end);
                }
            }

Reads segments of a file (in InputStream object)
and sends them to the OutputStream of a socket (out ObjectOutputStream object).

The problem is that the line:

out.writeObject(data);

always sends the content of data array read in the first iteration. This array is updated in every iteration but writeObject() method ignores these updates and writes the old value.

Does anybody have any explanation for this behavior or possible solution?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UrosVidojevic

ASKER

Thanks, it works now.