Solved

ObjectOutputStream.writeObject() behaves oddly

Posted on 2008-06-25
2
625 Views
Last Modified: 2008-06-25
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.
0
Comment
Question by:UrosVidojevic
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 21870890
thats because it is cached, call reset() to clear it.

0
 
LVL 7

Author Comment

by:UrosVidojevic
ID: 21870915
Thanks, it works now.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question