简体   繁体   English

FileOutputStream与ByteArrayOutputStream

[英]FileOutputStream vs ByteArrayOutputStream

I'm reading somebody else's code. 我正在读别人的代码。 Here's the gist of it. 这是它的要点。

A class compresses and decompresses files using GZIPInputStream and GZIPOutputStream. 类使用GZIPInputStream和GZIPOutputStream压缩和解压缩文件。

Here's a snippet of what goes on during compression. 这是压缩过程中发生的事情的片段。 inputFile and outputFile are instances of the class File . 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
...

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? 原始程序员在压缩期间选择FileOutputStream和解压缩期间ByteArrayOutputStream原因是什么? 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? 这是一个好主意吗?

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. :-P不,严重的是,除非您需要检查解压缩的数据,否则您可以使用BufferedOutputStream进行压缩和解压缩。

The ByteArrayOutputStream is more memory hogging since it stores the entire content in Java's memory (in flavor of a byte[] ). ByteArrayOutputStream占用的内存更多,因为它将整个内容存储在Java的内存中(以byte[]风格)。 The FileOutputStream writes to disk directly and is hence less memory hogging. FileOutputStream直接写入磁盘,因此占用的内存较少。 I don't see any sensible reason to use ByteArrayOutputStream in this particular case. 在这种特殊情况下,我没有看到任何明智的理由使用ByteArrayOutputStream It is not modifying the individual bytes afterwards. 之后不会修改单个字节。 It just get written unchanged to file afterwards. 之后它只会被写入文件。 It's thus an unnecessary intermediate step. 因此,这是一个不必要的中间步骤。

The programmer used FileInputStream during compression and used buffer when decompressing. 程序员在压缩期间使用FileInputStream并在解压缩时使用缓冲区。 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. 否则这需要很多内存并且可能产生OutOfMemeoryError。

I'd extract zip directly to temporary file and then rename the temporary file to its permanent name. 我将zip直接解压缩到临时文件,然后将临时文件重命名为其永久名称。 Finally block should care to delete the temporary file. 最后块应该关心删除临时文件。

ByteArrayOutputStream would give him/her a nice OutOfMemoryError ? ByteArrayOutputStream会给他/她一个不错的OutOfMemoryError吗?

Seriously, they were probably done at different times. 说真的,他们可能在不同的时间完成。 If you can, I'd consult the VCS logs. 如果可以,我会查阅VCS日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM