简体   繁体   English

在java中序列化可变对象

[英]Serializing mutable objects in java

I am programming a networked turn based java game, and use the java serialization api to serialize the game state each time a player does a move. 我正在编写一个基于网络的回合制java游戏,每次玩家移动时都使用java序列化api来序列化游戏状态。

To my annoyance, I discovered that the api assumes the objects are immutable. 令我烦恼的是,我发现api假设对象是不可变的。 This means that each time I try to send the updated game state, the old game state is received at the clients. 这意味着每次我尝试发送更新的游戏状态时,都会在客户端收到旧的游戏状态。

Is there some way to make the objectoutput/objectinputstream forget that an object has already been sent and make a new object each time? 有没有办法让objectoutput / objectinputstream忘记已经发送了一个对象并且每次都创建一个新对象?

ObjectOutputStream.reset() is what you are looking for: Oracle docs . 您正在寻找ObjectOutputStream.reset(): Oracle文档 It forces the stream to send updated objects. 它强制流发送更新的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM