如何将大对象转换为Java中的输入流
我正在尝试使用以下代码将对象转换为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,至少对于Sun Java运行时,
ObjectOutputStream
与bytearrayoutputstream
内部一起使用,可能用于两次通用处理,第二次通过优化了参考的编码。因此,无法使用此
ObjectOutputStream
实现来序列化任意大小的对象。您能做的就是从开源反传来重新实现
ObjectOutputStream
,在其中您替换嵌套的bytearrayOutputstream
eg eg by extureranga> rando> rando> randoccessfile
或实现bytearrayoutputstream
,没有2GB的人工限制。Note 如果您决定定义独立的实现:请考虑嵌套类的异步构成!
No, at least for the SUN java runtime,
ObjectOutputStream
works with aByteArrayOutputStream
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 nestedByteArrayOutputStream
e.g. by a temporaryRandomAccessFile
or an implementation ofByteArrayOutputStream
, 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!