Solved

ObjectOutputStream.writeObject() behaves oddly

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 85
hibernate example using maven 12 82
Find out all repos that a user is most active on Github 1 34
Java array sort 10 35
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

730 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