Java 套接字/序列化,对象不会更新 [英] Java socket/serialization, object won't update

查看:30
本文介绍了Java 套接字/序列化,对象不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于套接字的小程序.我正在使用类 ModelEvent 通过套接字传递信息.在 ModelEvent 内部,有一个类型为 (Object) 的变量 obect.

I am writing a little socket based program. I am using a class ModelEvent to pass information through the socket. inside ModelEvent, there's a variable obect of type (Object).

对象本身是一个带有一些值的二维数组.

The object itself is a 2D-array with some values.

object[1][2] = 2;
ModelEvent event = new ModelEvent("allo", object);
dispatchEvent(event);

object[2][3] = 2;
ModelEvent event2 = new ModelEvent("you", object);
dispatchEvent(event2);

比方说数组对象填充了值1,客户端收到第一个事件(event),数据是对的.通过数据发送的第二个事件不正确.它的数据与第一次调度中的数据相同.allo"和you"是看我是否没有两次阅读同一个事件,而答案不是.字符串是正确的,但对象不是,如果它已更新,则为事件.我在发送第二个事件之前遍历数组以查看它是否在服务器端更新,并且确实如此.但是在客户端,即使事件本身发生了变化,它仍然和第一次分派时一样.

Let's say that the array object is filled with the value 1. The first event (event) is received by the client, and the data is right. The second event sent though data is not right. Its data is the same as in the first dispatch. the "allo" and "you" is to see if I'm not reading the same event two times and the answer it's not. The string is right, but the object is not, event if it has been updated. I iterate through the array before sending the second event to see if it is updated on the server side, and it is. But on the client side, it still remain the same as in the first dispatch, even if the event itself is changed.

推荐答案

参见 ObjectOutputStream.reset.

重置将忽略已写入流的任何对象的状态.状态被重置为与新的 ObjectOutputStream 相同.流中的当前点被标记为重置,因此相应的 ObjectInputStream 将在同一点重置.先前写入流的对象不会被称为已在流中.它们将再次写入流.

Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.

/* prevent using back references */
output.reset();
output.writeObject(...);

在写入同一个对象之前调用 reset 以确保其更新的状态被序列化.否则,它只会使用反向引用到先前写入的对象及其过时的状态.

Call reset before writing the same object to ensure its updated state is serialized. Otherwise, it will merely use a back reference to the previously written object with its out-dated state.

或者,您也可以使用 ObjectOutputStream.writeUnshared 如下.

Or, you could alternatively use ObjectOutputStream.writeUnshared as follows.

写一个非共享"对象到 ObjectOutputStream.此方法与 writeObject 相同,不同之处在于它始终将给定对象作为新的唯一对象写入流中(与指向先前序列化实例的反向引用相反).

Writes an "unshared" object to the ObjectOutputStream. This method is identical to writeObject, except that it always writes the given object as a new, unique object in the stream (as opposed to a back-reference pointing to a previously serialized instance).

特别是:

  • 通过 writeUnshared 写入的对象始终以与新出现的对象(尚未写入流的对象)相同的方式进行序列化,无论该对象之前是否已写入.

  • An object written via writeUnshared is always serialized in the same manner as a newly appearing object (an object that has not been written to the stream yet), regardless of whether or not the object has been written previously.

如果使用 writeObject 写入先前已使用 writeUnshared 写入的对象,则先前的 writeUnshared 操作将被视为对单独对象的写入.换句话说,ObjectOutputStream 永远不会生成对通过调用 writeUnshared 写入的对象数据的反向引用.

If writeObject is used to write an object that has been previously written with writeUnshared, the previous writeUnshared operation is treated as if it were a write of a separate object. In other words, ObjectOutputStream will never generate back-references to object data written by calls to writeUnshared.

虽然通过 writeUnshared 编写对象本身并不能保证反序列化时对象的唯一引用,但它允许在一个流中多次定义单个对象,以便多次调用接收方对readUnshared 不会发生冲突.请注意,上述规则仅适用于使用 writeUnshared 编写的基础级对象,不适用于要序列化的对象图中的任何可传递引用的子对象.

While writing an object via writeUnshared does not in itself guarantee a unique reference to the object when it is deserialized, it allows a single object to be defined multiple times in a stream, so that multiple calls to readUnshared by the receiver will not conflict. Note that the rules described above only apply to the base-level object written with writeUnshared, and not to any transitively referenced sub-objects in the object graph to be serialized.

output.writeUnshared(...);

注意将其与 ObjectInputStream.readUnshared.

Note it's good practice to couple this with ObjectInputStream.readUnshared.

读取未共享"ObjectInputStream 中的对象.此方法与 readObject 相同,除了它阻止对 readObjectreadUnshared 的后续调用返回对通过此调用获得的反序列化实例的额外引用.

Reads an "unshared" object from the ObjectInputStream. This method is identical to readObject, except that it prevents subsequent calls to readObject and readUnshared from returning additional references to the deserialized instance obtained via this call.

特别是:

  • 如果调用 readUnshared 来反序列化反向引用(先前已写入流的对象的流表示),则将抛出 ObjectStreamException
  • 如果 readUnshared 成功返回,则任何后续尝试反序列化对由 readUnshared 反序列化的流句柄的反向引用都将导致 ObjectStreamException被抛出.
  • If readUnshared is called to deserialize a back-reference (the stream representation of an object which has been written previously to the stream), an ObjectStreamException will be thrown
  • If readUnshared returns successfully, then any subsequent attempts to deserialize back-references to the stream handle deserialized by readUnshared will cause an ObjectStreamException to be thrown.

通过 readUnshared 反序列化对象会使与返回的对象关联的流句柄无效.请注意,这本身并不总是保证 readUnshared 返回的引用是唯一的;反序列化的对象可能会定义一个 readResolve 方法,该方法返回一个对其他方可见的对象,或者 readUnshared 可能会返回一个 Class 对象或 enum 可在别处获得的常量在流中或通过外部手段.如果反序列化对象定义了一个 readResolve 方法并且该方法的调用返回一个数组,则 readUnshared 返回该数组的浅克隆;这保证了返回的数组对象是唯一的,并且不能从 readObjectObjectInputStream 上的 readUnshared 的调用中获得第二次,即使底层数据流已被操作.

Deserializing an object via readUnshared invalidates the stream handle associated with the returned object. Note that this in itself does not always guarantee that the reference returned by readUnshared is unique; the deserialized object may define a readResolve method which returns an object visible to other parties, or readUnshared may return a Class object or enum constant obtainable elsewhere in the stream or through external means. If the deserialized object defines a readResolve method and the invocation of that method returns an array, then readUnshared returns a shallow clone of that array; this guarantees that the returned array object is unique and cannot be obtained a second time from an invocation of readObject or readUnshared on the ObjectInputStream, even if the underlying data stream has been manipulated.

obj = input.readUnshared();

这篇关于Java 套接字/序列化,对象不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆