Java:使用不带序列化的 ObjectOutputStream

发布于 2024-08-04 07:57:56 字数 137 浏览 9 评论 0原文

有时,我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云巢 2024-08-11 07:57:57

仅支持的对象
java.io.Serialized接口可以是
写入流。

-- ObjectOutputSteam 文档

但是,您可以避免所有这一切都是通过使用 javax.imageio 中的类之一实现的。具体来说 ImageIO.write(RenderedImage, String, OutputStream) 方法,因为 BufferedImage 实现了 渲染图像。然后,您可以使用 ImageIO.read(InputStream),它返回一个BufferedImage

不过,您可能需要不同的 OutputSteam 类型。除了普通的OutputStream之外,还有几个特殊的ImageOutputStream

编辑:我之前错过了这个:
要获取中间参数的有效字符串列表,可以调用 ImageIO.getWriterFormatNames()

Only objects that support the
java.io.Serializable interface can be
written to streams.

-- ObjectOutputSteam docs

However, you could avoid all this by using one of the classes in javax.imageio. Specifically the ImageIO.write(RenderedImage, String, OutputStream) method, because BufferedImage implements RenderedImage. You can then read it back out with ImageIO.read(InputStream), which returns a BufferedImage.

You'll probably want a different OutputSteam type, though. In addition to the normal OutputStreams, there are several special ImageOutputStreams.

Edit: I missed this before:
To get a list of valid strings for the middle argument, you can call ImageIO.getWriterFormatNames()

缱绻入梦 2024-08-11 07:57:57

处理不可序列化对象的一种方法是跳过它们。您可以扩展ObjectOutputStream并实现replaceObject方法来检查对象是否可序列化。

public class MyOOS extends ObjectOutputStream {
    public MyOOS(OutputStream out) throws IOException {
        super(out);
        enableReplaceObject(true);
    }

    @Override
    protected Object replaceObject(Object obj) throws IOException {
        if ((obj instanceof Serializable))
            return obj;
        System.err.println("Skipping serialization of "+obj);
        return null;
    }
}

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.

public class MyOOS extends ObjectOutputStream {
    public MyOOS(OutputStream out) throws IOException {
        super(out);
        enableReplaceObject(true);
    }

    @Override
    protected Object replaceObject(Object obj) throws IOException {
        if ((obj instanceof Serializable))
            return obj;
        System.err.println("Skipping serialization of "+obj);
        return null;
    }
}
明媚殇 2024-08-11 07:57:57

不,这就像说:“我想将对象显示为文本,但不知道如何将其转换为字符串。”

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 implement Serializable, 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 use ObjectOutputStream. Images in particular are likely to have their own "native format" which ImageIO can deal with (as R. Bemrose noted).

揪着可爱 2024-08-11 07:57:57

编写一个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).

嘦怹 2024-08-11 07:57:57

您可能可以选择使用不同的序列化机制。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文