gpt4 book ai didi

Java套接字/序列化,对象不会更新

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:03 25 4
gpt4 key购买 nike

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

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

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”是为了看我是否没有两次阅读同一事件,而答案不是。字符串正确,但对象不正确,如果已更新则为事件。我在发送第二个事件之前遍历数组以查看它是否在服务器端更新,确实如此。但在客户端,即使事件本身发生了变化,它仍然与第一次派发时相同。

最佳答案

参见 ObjectOutputStream.reset .

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 以确保其更新状态被序列化。否则,它只会使用一个向后引用到先前写入的对象及其过时状态。

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

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).

Specifically:

  • 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.

  • 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.

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 结合使用.

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.

Specifically:

  • 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.

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套接字/序列化,对象不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12341086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com