序列化中readObject / writeObject的使用 [英] Uses of readObject/writeObject in Serialization
问题描述
我正在浏览这篇文章以了解更多信息关于Java序列化过程。当使用 readObject / writeObject
时,我可以看到两个用例:
I was going through this article to understand more about Java Serialization process. When it comes to uses of readObject/writeObject
I could see two use cases:
- 我们可以使用
writeObject
在序列化之前加密字节码。从安全的角度来看,这是件好事。 - 我们可以使用
readObject
来执行需要执行的任何特定代码段在反序列化之后立即从#1开始,我们甚至可以使用readObject
来解密序列化对象时被盗取的字节代码。
- We can use
writeObject
to encrypt the byte code before it gets serialized. From the security point of view, that's good thing. - we can use
readObject
to execute any specific piece of code that need to execute immediately after deserialization, and off course from poin#1, we can even usereadObject
to decrypt the byte code that was excrypted while serializing the object.
在编写自定义readObject / writeObject方法时序列化/反序列化对象时是否还有其它实际情况?或者,如果你能指出我可以看到readObject / writeObject的一些体面和实际用途的任何地方?
Is there any other practical scenario you've come across while serializing/deserializing objects by writing customr readObject/writeObject method? Or If you could point me to any place where I could see some decent and practical uses of readObject/writeObject?
推荐答案
自定义 readObject
方法在对象被反序列化后需要初始化瞬态(非序列化)字段时也很有用。
Custom readObject
methods are also useful when you need to initialize transient (non-serialized) fields after the object has been deserialized.
BTW,请查看 Effective Java,Chapter 11 (我不确定第2版中的章节/项目编号是什么。)。这是一篇关于序列化的精彩读物。
BTW, check out Effective Java, Chapter 11 (I'm not sure what the chapter/item number is in the 2nd ed.). It's an excellent read on serialization.
这篇关于序列化中readObject / writeObject的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!