java - try-with-resources 中的 DigestInputStream 是否会关闭原始 InputStream?
问题描述
如果我在 try-with-resources 块中InputStream
声明 a ,是否需要显式关闭原始文件?DigestInputStream
例子:
InputStream is = ...;
MessageDigest md = ...;
try (final DigestInputStream digestInputStream = new DigestInputStream(is, md)) {
// Read the stream...
}
我是否需要手动关闭?
解决方案
因为在 try-with-resources 块中声明它DigestInputStream
时AutoCloseable
,您不需要手动关闭它。
文件来自AutoCloseable
:
{@code AutoCloseable} 对象的 {@link #close()} 方法在退出已在资源规范标头中声明对象的 {@code try}-with-resources 块时自动调用。
此外,FilterInputStream
覆盖了close
关闭 used 的方法InputStream
。
推荐阅读
- python - 为什么 Numpy 中的 0d 数组被视为标量?
- javascript - React - 如何在不使用 setTimeout 的情况下将前端与后端请求同步?
- rgraph - 带有镜像 X 轴的散点图
- linux - 仅在不匹配时比较两个文件并将输出存储在文件中
- encryption - 将单行 RSA 私有 ssh 密钥转换为多行
- c# - ASP.NET Core 2.2 防止对文档/文件的直接 URL 访问
- angular - US Counties map in Highmaps for Angular throws compilation error
- c# - 使用 IComparable 和 IComparer 的多重排序规则?
- c# - Tibco ems 多个消费者
- android - asynctask 函数无法识别结果值