包含两个文件的单个输入流。我想分割这些文件
在java中,我有一个包含多个文件的inputstream
(可能是sequenceinputstream
)。我想使用java分离这些文件。 java中有没有可用的解决方案?
In java I'm having an inputstream
(maybe sequenceinputstream
) containing more than one file. I want to separate those files using java. Is there any solution available in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的答案是:你不能/不应该。
复杂的答案:如果您正在处理
SequenceInputStream
,您可以使用此反射 hack 访问底层流:但我会非常小心使用这样的东西。
The simple answer is: you can't / shouldn't.
The complicated answer: in case you are dealing with a
SequenceInputStream
, you can access the underlying streams using this reflection hack:But I would be very careful about using something like this.
这取决于您拥有什么类型的文件,如果有任何保留的序列指示文件的结尾和/或开头(例如 HTML 或 XML 等),那么您可以将它们分开,否则我看不到任何方法这。
This depends on what kind of files you have, if there is any reserved squence which indicates the End and/or the beginning of the file like HTML or XML etc. then you can separate them, otherwise i don't see any way to do this.
您需要有某种方法来确定一个文件的结束位置和下一个文件的开始位置 - 大多数文件保留前几个字节作为“身份”标记。请记住,Java 坚持使用 SIGNED 字节类,这可能会导致运行时识别这些字节类变得混乱。
但是,您无法真正拆分流。因此,您必须通过普通的 byte[] 数组来暂存数据。我将从父流中读取 4K(对于文件)或 64K(对于网络)的块,因为底层本机调用通常使用这些大小,将块存储在列表中,然后在最后为每个文件创建一个 ByteArrayInputStream 。
You need to have some way of determining where one file ends and the next begins- most files reserve the first few bytes as an "identity" tag. Bear in mind that Java's insistence on having a SIGNED byte class can mess with identifying these at runtime.
However, you can't really split streams. So, you'll have to stage your data through ordinary byte[] arrays. I'd read from the parent stream in chunks of 4K (for files) or 64K (for the network) since the underlying native calls usually use those sizes, store the chunks in lists, and then create a ByteArrayInputStream for each file at the end.