3

I've successfully used ObjectOutputStream and ObjectInputStream to serialize and deserialize objects for a server and client I am writing. The server and client are usually on two different machines, but since It's going to be a turn based card game, I also want the users to play locally and then both the client and the server will run on the same machine.

Now, I'm writing an alternative implementation of my connection interface. The interface contains the methods such as void sendToServer(Object) and Object receiveFromServer()

Usually the sendToServer(Object) function will just send the object through an ObjectOutputStream, but since the objects are on the same machine, I could just create a queue of Objects and push and pop each time the send or receive is called. The problem, however, is that I don't want the client to have a reference to the object the server uses, I want it to be a copy. And i don't have a copy constructor and the objects are not cloneable.

What would you do in my situation?

bobbaluba
  • 3,584
  • 2
  • 31
  • 45
  • If playing on the same machine, does it run in the same or separate JVM? – Steve Kuo Mar 19 '12 at 17:42
  • Why not just add a copy constructor or be cloneable? – Steve Kuo Mar 19 '12 at 17:42
  • 1
    And tell me again why can you not simply serialize the object in memory and then deserialize them back again to make copies of them using object input and output streams? – Edwin Dalorzo Mar 19 '12 at 17:47
  • @edalorzo: And tell me again why you didn't make that an answer rather than a comment? :) It's possible the OP hasn't thought of that. – Tom Anderson Mar 19 '12 at 17:50
  • @Tom I see your point! It is just that the question says "without ObjectOutputStream" so I thought that I must have missed something in the question, namely, the reason why it cannot be used. I am looking for confirmation that this is a requirement to avoid suggesting anything innapropriate. Otherwise, my question could be the alternative, as you well pointed out :) – Edwin Dalorzo Mar 19 '12 at 17:55
  • @edalorzo That would work. But how would I do it? Isn't ObjectOutputStream supposed to decorate an OutputStream? How would I get it to output into memory and not on some socket or file? – bobbaluba Mar 19 '12 at 17:55
  • @bobbaluba Your ObjectOutputStream decorates any kind of OutputStream, so it can also decoare a ByteArrayOutputStream. The same thing with ObjectInputStream and ByteArrayInputStream. I have shared an example as an answer for your perusal. – Edwin Dalorzo Mar 19 '12 at 18:08

2 Answers2

5

Take a look at ByteArrayInputStream and ByteArrayOutputStream. Use them to serialize your objects into memory and get them back from them.

ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bOut);
out.writeObject(new Jedi("Obiwan"));
out.close();

byte[] payload = bOut.toByteArray();

ByteArrayInputStream bIn = new ByteArrayInputStream(payload);
ObjectInputStream in = new ObjectInputStream(bIn);
Jedi jedi = (Jedi) in.readObject();
in.close();

System.out.println(jedi);

In this case you can use a memory structure to put your bytes, instead of sending them through the network.

I hope that helps!

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
0

Usually the sendToServer(Object) function will just send the object through an ObjectOutputStream, but since the objects are on the same machine, I could just create a queue of Objects and push and pop each time the send or receive is called ...

But that leads to all the problems you have described. So why do anything? Leave it the way it is.

user207421
  • 305,947
  • 44
  • 307
  • 483