Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ObjectOutputStream.writeObject() behaves oddly

Posted on 2008-06-25
2
Medium Priority
?
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

636 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