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

查看:192
本文介绍了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).

对象本身是一个带有一些值的2D数组。

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.收到第一个事件(事件)由客户端,数据是正确的。通过数据发送的第二个事件是不对的。其数据与第一次发送时的数据相同。 allo和you是为了看我是不是两次读同一个事件,答案不是。字符串是正确的,但对象不是,如果已更新,则为event。我在发送第二个事件之前迭代数组,以查看它是否在服务器端更新,它是。但在客户端,它仍然与第一次发送时相同,即使事件本身已更改。

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。重置


重置将忽略已写入流的任何对象的状态。状态重置为与新的 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.

或者,您可以选择使用< a href =http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html#writeUnshared(java.lang.Object) =noreferrer> 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相同,不同之处在于它阻止后续调用 readObject 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.

具体来说:


  • 如果 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可以返回对象或枚举常量可在流中或通过外部方式获得。如果反序列化对象定义 readResolve 方法并且该方法的调用返回一个数组,则 readUnshared 返回一个浅层克隆那个阵列;这可以保证返回的数组对象是唯一的,并且无法在 readObject ObjectInputStream ,即使基础数据流已被操纵。

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天全站免登陆