如何将大对象转换为Java中的输入流

发布于 2025-01-31 01:26:34 字数 488 浏览 2 评论 0原文

我正在尝试使用以下代码将对象转换为InputStream。

 try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        ObjectOutputStream dataOutput = new ObjectOutputStream(byteArrayOutputStream);
        dataOutput.writeObject(dummyObject);
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    } catch (Exception exception) {
    }

由于对象的大小可能略大于2GB,因此ByTearRayOutputStream会抛出OutofMemory。

有什么办法将大对象转换为Java中的输入流?

I am trying to convert an object to inputstream using the below code.

 try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        ObjectOutputStream dataOutput = new ObjectOutputStream(byteArrayOutputStream);
        dataOutput.writeObject(dummyObject);
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    } catch (Exception exception) {
    }

since the size of the object can be slightly greater than 2gb, ByteArrayOutputStream is throwing OutOfMemory.

Any way to convert a big object to input stream in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

八巷 2025-02-07 01:26:34

不,至少对于Sun Java运行时,ObjectOutputStreambytearrayoutputstream内部一起使用,可能用于两次通用处理,第二次通过优化了参考的编码。

因此,无法使用此ObjectOutputStream实现来序列化任意大小的对象。

您能做的就是从开源反传来重新实现ObjectOutputStream,在其中您替换嵌套的bytearrayOutputstream eg eg by exture ranga> rando> rando> randoccessfile或实现bytearrayoutputstream,没有2GB的人工限制。

Note 如果您决定定义独立的实现:请考虑嵌套类的异步构成!

No, at least for the SUN java runtime, ObjectOutputStream works with a ByteArrayOutputStream internally, probably for a two-pass processing, where the second pass optimizes the encoding of references.

So, there is no way to serialize Objects of arbitrary size with this ObjectOutputStream implementation.

What you can do is re-implement the ObjectOutputStream from an open-source-version, where you replace the nested ByteArrayOutputStream e.g. by a temporary RandomAccessFile or an implementation of ByteArrayOutputStream, that does not have that artificial limit of 2GB.

Note In case you decide to define an independent implementation: Well consider the asynchronous instatiation of nested classes!

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