如何避免关闭传递给我包装在 Reader 流中的方法的 InputStream?
我正在创建一个接受单个 InputStream
作为参数的 Java 方法。为了方便使用基于字符的流,我将提供的 InputStream
包装在方法实现的开头,如下所示:
public void doStuff(InputStream inStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
...
}
由于 InputStream
(inStream< /code>) 传递给我的方法,我不想关闭它......因为我认为这应该是调用我的方法的客户端的责任(这个假设正确吗?)。但是,我确实认为我应该关闭我创建的 BufferedReader;但这样做时,我相信它会自动关闭所有其他组成的流,包括
inStream
。
有没有人找到一种方法可以让我关闭我创建的 BufferedReader 和 InputStreamReader,同时不关闭传递给我的方法的 InputStream?也许有一种方法可以在包装之前复制提供的 InputStream
?谢谢
I'm creating a Java method that accepts a single InputStream
as an argument. For the convenience of working with a character-based stream, I wrap the provided InputStream
at the start of the method implementation as follows:
public void doStuff(InputStream inStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
...
}
Since the InputStream
(inStream
) is passed to my method, I don't want to close it ... as I think that should be the responsibility of the client calling my method (is this assumption correct?). However, I do think that I should close the BufferedReader
that I created; but in doing so, I believe it will automatically close all the other composed streams including the inStream
.
Does anyone see a way for me to close the BufferedReader
and InputStreamReader
that I created while not closing the InputStream
passed to my method? Maybe there is a way to make a copy of the provided InputStream
before I wrap it? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您不希望关闭底层读取器时,您不需要关闭 BufferedReader 或 InputStreamReader 或可能大多数读取器实现。
这些读取器不持有调用
close()
会释放的任何资源,并且当您删除对这些资源的引用时,垃圾收集器无论如何也不会释放这些资源(例如本机资源或静态变量中的值)方法末尾的读者。与本机资源通信的底层输入流(例如
FileInputStream
或从 URL 获取的流)必须关闭以释放这些本机资源。对于
Writer
来说,不同之处在于close()
通常调用flush()
。但随后,您可以直接调用flush()
。You do not need to close a
BufferedReader
orInputStreamReader
or probably most reader implementations, when you do not wish to close the underlying reader.These readers do not hold any resources that a call to
close()
would make free and which the garbage collector would not make free anyway (e.g. native resources or values in static variables) when you drop the reference to the reader at the end of your method.The underlying input streams that communicate with native resources, e.g.
FileInputStream
or streams obtained from URLs, must be closed to free those native resources.For
Writer
, there's a difference in thatclose()
usually callsflush()
. But then, you can callflush()
directly.NoCloseInputStream
您可能会在类路径中找到此类。 hibernate 和 sun 的实现是已知的。如果是这样,您可以使用此类作为包装器。祝你好运。
CloseIgnoringInputStream
这个是 apache-poi 自带的。
CloseSuppressingInputStream
这个来自hibernate。
KeepAliveInputStream
这个也有一个无操作关闭方法。
StreamUtils.nonClosing() - Spring
从现在开始应该清楚:不关闭输入流是一个常见的要求。这个来自春天。
这就是我通常的类路径的全部内容。
NoCloseInputStream
You might found this class in your classpath. Implementations are known from hibernate and sun. If so, you could use this class as a wrapper. Good luck.
CloseIgnoringInputStream
This one comes with apache-poi.
CloseSuppressingInputStream
This one is from hibernate.
KeepAliveInputStream
This one has a no-op close method too.
StreamUtils.nonClosing() - Spring
From now it should be clear: Not closing a inputstream is a common requirement. This one comes from spring.
Thats all from my usual classpath.
就我个人而言,我会通过更改方法的签名以要求传入 BufferedReader (或 Reader)来避免该问题。
Personally I would just avoid the problem by changing the signature of your method to require that a BufferedReader (or Reader) be passed in.
我会尝试覆盖
close
方法:有点疯狂,但它应该可以工作。不过,我不确定这是最好的方法。
我觉得这个问题很有趣,希望有专家给出意见。
What I'd try would be overriding the
close
method:Kind of crazy, but it should work. Though, I'm not sure this is the best approach.
I think this is an interesting question, so I hope someone expert gives his/her opinion.