FileOutputStream 与 ByteArrayOutputStream

发布于 2024-10-10 07:28:53 字数 1041 浏览 3 评论 0原文

我正在阅读别人的代码。这是它的要点。

类使用 GZIPInputStream 和 GZIPOutputStream 压缩和解压缩文件。

这是压缩过程中发生的事情的一个片段。 inputFileoutputFile 是类 File 的实例。

FileInputStream fis = new FileInputStream(inputFile);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFile));

//the following function copies an input stream to an output stream
IOUtils.copy(fis,gzos);

//outputFile is the compressed file
...

现在,这是减压过程中发生的情况。

GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(inputFile));
ByteArrayOutputStream baos = new ByteArrayOutputStream();

//copies input stream to output stream
IOUtils.copy(gzis,baos);

//this method does as its name suggests
FileUtils.writeByteArrayToFile(outputFile, baos.toByteArray());

//outputFile is the decompressed file
...

原始程序员在压缩期间选择 FileOutputStream 而在解压期间选择 ByteArrayOutputStream 的可能原因是什么?这让我很困惑。

除非有充分的理由,否则我认为我将更改它们以保持一致,以避免将来出现混乱。这是个好主意吗?

I'm reading somebody else's code. Here's the gist of it.

A class compresses and decompresses files using GZIPInputStream and GZIPOutputStream.

Here's a snippet of what goes on during compression. inputFile and outputFile are instances of the class File.

FileInputStream fis = new FileInputStream(inputFile);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFile));

//the following function copies an input stream to an output stream
IOUtils.copy(fis,gzos);

//outputFile is the compressed file
...

Now, here's what's going on during decompression.

GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(inputFile));
ByteArrayOutputStream baos = new ByteArrayOutputStream();

//copies input stream to output stream
IOUtils.copy(gzis,baos);

//this method does as its name suggests
FileUtils.writeByteArrayToFile(outputFile, baos.toByteArray());

//outputFile is the decompressed file
...

What's a possible reason the original programmer chose FileOutputStream during compression and ByteArrayOutputStream during decompression? It confuses me.

Unless there's a good reason, I think I'm changing them to be consistant to avoid future confusion. Is this a good idea?

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

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

发布评论

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

评论(4

心如荒岛 2024-10-17 07:28:53

呵呵,听起来他们从不同来源复制并粘贴了代码? :-P 不,说真的,除非您需要检查解压缩的数据,否则您可以使用 BufferedOutputStream 进行压缩和解压缩。

Heh, sounds like they copied and pasted code from different sources? :-P No, seriously, unless you need to inspect the decompressed data, you can just use a BufferedOutputStream for both compression and decompression.

时光匆匆的小流年 2024-10-17 07:28:53

ByteArrayOutputStream 更占用内存,因为它将整个内容存储在 Java 内存中(类似于 byte[])。 FileOutputStream 直接写入磁盘,因此占用内存较少。在这种特殊情况下,我没有看到任何使用 ByteArrayOutputStream 的合理理由。之后它不会修改各个字节。之后它只是原封不动地写入文件。因此,这是一个不必要的中间步骤。

The ByteArrayOutputStream is more memory hogging since it stores the entire content in Java's memory (in flavor of a byte[]). The FileOutputStream writes to disk directly and is hence less memory hogging. I don't see any sensible reason to use ByteArrayOutputStream in this particular case. It is not modifying the individual bytes afterwards. It just get written unchanged to file afterwards. It's thus an unnecessary intermediate step.

日记撕了你也走了 2024-10-17 07:28:53

程序员在压缩时使用FileInputStream,在解压时使用buffer。我认为原因是,如果您在读取文件时失败,则不会发生任何不良情况。你只是失败了并且抛出了异常。

如果您在解压缩时失败并且您已经开始写入文件,则该文件已损坏。所以他决定先写入缓冲区,然后在解压完成后将缓冲区写入磁盘。如果您处理的是相对较小的文件,则此解决方案是可以的。否则,这需要大量内存并可能产生 OutOfMemeoryError。

我将 zip 直接解压到临时文件,然后将临时文件重命名为其永久名称。最后块应该注意删除临时文件。

The programmer used FileInputStream during compression and used buffer when decompressing. I think that the reason was that if you are failing duinr reading the file nothing bad happens. You just fail and a exception is thrown.

If you are failing while decompressing and you already started writing to file the file is corrupted. So he decided to write buffer first and then when decompression is completed to writer the buffer on disk. This solution is OK if you are dealing with relatively small files. Otherwise this requires to much memory and could produce OutOfMemeoryError.

I'd extract zip directly to temporary file and then rename the temporary file to its permanent name. Finally block should care to delete the temporary file.

吃素的狼 2024-10-17 07:28:53

ByteArrayOutputStream 会给他/她一个很好的 OutOfMemoryError

说真的,它们可能是在不同时间完成的。如果可以的话,我会查阅 VCS 日志。

ByteArrayOutputStream would give him/her a nice OutOfMemoryError?

Seriously, they were probably done at different times. If you can, I'd consult the VCS logs.

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