Solved

ObjectOutputStream.writeObject() behaves oddly

Posted on 2008-06-25
2
603 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now