Java:使用不带序列化的 ObjectOutputStream
有时,我想使用 ObjectOutputStream 向文件写入内容或通过网络发送小图像。但是 BufferedImage 和许多其他类没有实现 java.io.Serialized,然后 Stream 就会取消写入。有办法避免吗?
谢谢,马丁
Sometimes, I want to use an ObjectOutputStream
to write something to a file or sending a little image over the network. But BufferedImage
and many other classes don't implement java.io.Serializable
and then the Stream cancels writing. Is there a way avoid that?
Thanks, Martijn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
-- ObjectOutputSteam 文档
但是,您可以避免所有这一切都是通过使用 javax.imageio 中的类之一实现的。具体来说
ImageIO.write(RenderedImage, String, OutputStream)
方法,因为BufferedImage
实现了渲染图像
。然后,您可以使用ImageIO.read(InputStream)
,它返回一个BufferedImage
。不过,您可能需要不同的
OutputSteam
类型。除了普通的OutputStream之外,还有几个特殊的ImageOutputStream
。编辑:我之前错过了这个:
要获取中间参数的有效字符串列表,可以调用
ImageIO.getWriterFormatNames()
-- ObjectOutputSteam docs
However, you could avoid all this by using one of the classes in
javax.imageio
. Specifically theImageIO.write(RenderedImage, String, OutputStream)
method, becauseBufferedImage
implementsRenderedImage
. You can then read it back out withImageIO.read(InputStream)
, which returns aBufferedImage
.You'll probably want a different
OutputSteam
type, though. In addition to the normalOutputStream
s, there are several specialImageOutputStream
s.Edit: I missed this before:
To get a list of valid strings for the middle argument, you can call
ImageIO.getWriterFormatNames()
处理不可序列化对象的一种方法是跳过它们。您可以扩展ObjectOutputStream并实现replaceObject方法来检查对象是否可序列化。
One way to deal with unserializable objects is simply skip them. You can extends ObjectOutputStream and implement replaceObject method that checks if object is serializable.
不,这就像说:“我想将对象显示为文本,但不知道如何将其转换为字符串。”
Serialized
的全部目的是说“我知道如何序列化为流!” - 如果我们不需要它,我们就不会拥有它。现在,如果您有一个实现
Serialized
的对象,但包含一些本身不实现Serialized
的对象,但您可以找到某种方法手动序列化,您始终可以自己自定义容器对象的序列化。基本上,
ObjectOutputStream
是为 Java 的序列化框架设计的。如果您不想使用序列化框架,请不要使用ObjectOutputStream
。特别是图像可能有自己的“本机格式”,ImageIO
可以处理(正如 R. Bemrose 指出的那样)。No. That's like saying, "I want to display an object as text, but don't know anything about how to convert it into a string."
The entire purpose of
Serializable
is to say "I know how to be serialized to a stream!" - if we didn't need it, we wouldn't have it.Now if you have an object which implements
Serializable
but contains something which itself doesn't implementSerializable
, but which you could work out some way of serializing by hand, you could always customize the serialization of the container object yourself.Basically
ObjectOutputStream
is designed for Java's serialization framework. If you don't want to use the serialization framework, don't useObjectOutputStream
. Images in particular are likely to have their own "native format" whichImageIO
can deal with (as R. Bemrose noted).编写一个ObjectOutputStream子类,调用enableReplaceObject,然后重写replaceObject(Object)。您可能还需要一个配套的 ObjectInputStream 子类,通过重写resolveObject(Object) 来执行相同的操作。
Write an ObjectOutputStream subclass, call enableReplaceObject, and then override replaceObject(Object). You probably also need a companion ObjectInputStream subclass does the same by overriding resolveObject(Object).
您可能可以选择使用不同的序列化机制。 JBoss Serialization 是标准 java.io 序列化的直接替代品,尽管由于序列化格式不同,读者和作者都需要使用相同的机制。
JBoss Serialization 不要求类实现 java.io.Serialized,但是如果对象不是显式可序列化的,则不能保证对象能够在该过程中存活下来。
It may be an option for uou to use a different serialization mechanism. JBoss Serialization is a drop-in replacement for standard java.io serialization, although because the serialization format is different, both readers and writers need to use the same mechanism.
JBoss Serialization does not require classes to implement
java.io.Serializable
, however there's no guarantee that the objects will survive the process if they aren't explicitly Serializable.