File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Writing an int[][] via ObjectOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing an int[][] via ObjectOutputStream" Watch "Writing an int[][] via ObjectOutputStream" New topic

Writing an int[][] via ObjectOutputStream

Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
After about a year of absense, I return to you gentlemen, again at my wits' end.

The setup:

Server tells client the initial state of the board (int[][])
Client updates board to match
while( true ){
..Via mouse input, coordinates of a move are sent to server.
..Server updates board
..sends client the new board
..client updates board to match

The really bizzare thing is as follows: the server board is changed, this is verified by printing the int[][]. It correctly updates every time. Then, I send that very int[][], but when the client reads it, it does not reflect the change. I know it reads, because it blocks until it should read (verified by outputs). But the board is always identical to the first int[][] that stream sent, no matter how manny subsequent changes have been made on the server side.
Things get stranger! When I instead sent a constantly incrementing Integer object, on the same stream, it worked perfectly, and was different every time.

The relevant code:

if I print the int[][] just before server sends it, and just after client receives it, they're not the same. Rather, client keeps receiving the same array over and over. And again, this is not so with other objects (I tested sending Integers and Strings)

[ October 18, 2005: Message edited by: Nick George ]

I've heard it takes forever to grow a woman from the ground
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Nick,

Welcome back!

Easy question (I like easy questions...) It turns out that an ObjectOutputStream will only send a given object once. If you send the same object again, it won't send a new copy: it will instead send a message that says "that same object again, thanks." This is how infinite loops are prevented when pickling cyclic object graphs.

Anyway, the solution is simple: before re-sending the board, call reset() on the ObjectOutputStream. This will discard any memory of having previously sent any objects, so all objects will be sent in the entirety.

[Jess in Action][AskingGoodQuestions]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Wow, Thanks a lot. I spent a very long time working on what would have been solved in 10 minutes by asking here or, even, carefully reading the API. Well, it's Java's fault for not putting that in flashing red letters.

I really appreciate it.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
I like the part that the two other objects I chose to test were String and Integer- both immutable, so I was forced to make new objects every time.
I agree. Here's the link:
subject: Writing an int[][] via ObjectOutputStream
It's not a secret anymore!