我需要关闭 ByteArrayInputStream 吗?

发布于 2024-10-19 00:12:06 字数 528 浏览 7 评论 0原文

简短的问题,

我在一些旧代码中看到 ByteArrayInputStream 创建如下:

new BufferedReader(new InputStreamReader(new ByteArrayInputStream(somebytes)));

然后使用 BufferedReader 逐行读出 somebytes .
一切正常,但我注意到 BufferedReader 从未关闭。
这一切都在长时间运行的 websphere 应用程序中进行,somebytes 并不是很大(最多 200k),它每周只被调用几次,而且我们没有遇到任何明显的内存泄漏。所以我希望所有对象都被成功垃圾收集。

我总是(一次)了解到输入/输出流需要在 finally 语句中关闭。 ByteStreams 是这条规则的例外吗?

亲切的问候 杰罗恩.

Short question,

I saw in some old code where a ByteArrayInputStream was created like:

new BufferedReader(new InputStreamReader(new ByteArrayInputStream(somebytes)));

And then the BufferedReader is used to read out somebytes line by line.
All working fine, but I noticed that the BufferedReader is never closed.
This is all working in a long running websphere application, the somebytes are not terrible big (200k most), it is only invoked a few times a week and we're not experiencing any apparent memory leaks. So I expect that all the objects are successfully garbage collected.

I always (once) learned that input/output streams need to be closed, in a finally statement. Are ByteStreams the exception to this rule?

kind regards
Jeroen.

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

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

发布评论

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

评论(4

这样的小城市 2024-10-26 00:12:06

您不必关闭 ByteArrayInputStream ,当它没有被任何变量引用时,垃圾收集器将释放流和一些字节(当然假设它们没有被引用)其他地方)。

但是关闭每个流始终是一个好习惯,事实上,也许创建流的实现将来会发生变化,您将读取文件而不是原始字节?另外,静态代码分析工具(如 PMD 或 FindBugs)(请参阅评论)很可能会抱怨。

如果您厌倦了关闭流并被迫处理不可能的 IOException,您可以使用 IOUtils

IOUtils.closeQuietly(stream);

You don't have to close ByteArrayInputStream, the moment it is not referenced by any variable, garbage collector will release the stream and somebytes (of course assuming they aren't referenced somewhere else).

However it is always a good practice to close every stream, in fact, maybe the implementation creating the stream will change in the future and instead of raw bytes you'll be reading file? Also static code analyzing tools like PMD or FindBugs (see comments) will most likely complain.

If you are bored with closing the stream and being forced to handle impossible IOException, you might use IOUtils:

IOUtils.closeQuietly(stream);
半步萧音过轻尘 2024-10-26 00:12:06

关闭读者始终是一个好习惯。然而,不关闭 ByteArrayInputStream 不会产生那么严重的潜在负面影响,因为您没有访问文件,只是访问内存中的字节数组。

It is always good practice to close your readers. However not closing a ByteArrayInputStream does not have as heavy of a potential negative effect because you are not accessing a file, just a byte array in memory.

倾`听者〃 2024-10-26 00:12:06

正如 @TomaszNurkiewicz 提到的,关闭打开的流总是好的。让它自己执行 try 块的另一个好方法。将 try 与资源一起使用,例如......

try ( InputStream inputStream = new ByteArrayInputStream(bytes); Workbook workBook = new XSSFWorkbook(inputStream)) { 

这里 Workbook 和 InputStream 都实现 Closeable Interface,因此一旦 try 块完成(正常或突然),流肯定会关闭。

As @TomaszNurkiewicz mentioned it's always good to close the opened stream. Another good way to let it do the try block itself. Use try with resource like.......

try ( InputStream inputStream = new ByteArrayInputStream(bytes); Workbook workBook = new XSSFWorkbook(inputStream)) { 

here Workbook and InputStream both implements Closeable Interface so once try block completes ( normally or abruptly), stream will be closed for sure.

妞丶爷亲个 2024-10-26 00:12:06

资源需要在finally(或同等方式)中关闭。但是如果你只有一些字节,那没关系。尽管在编写时,请注意在快乐的情况下flush

Resources need to be closed in a finally (or equivalent). But where you just have some bytes, no it doesn't matter. Although when writing, be careful to flush in the happy case.

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