如何避免关闭传递给我包装在 Reader 流中的方法的 InputStream?

发布于 2024-09-09 06:27:13 字数 626 浏览 8 评论 0原文

我正在创建一个接受单个 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 技术交流群。

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

发布评论

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

评论(4

嘴硬脾气大 2024-09-16 06:27:13

当您不希望关闭底层读取器时,您不需要关闭 BufferedReader 或 InputStreamReader 或可能大多数读取器实现。

这些读取器不持有调用 close() 会释放的任何资源,并且当您删除对这些资源的引用时,垃圾收集器无论如何也不会释放这些资源(例如本机资源或静态变量中的值)方法末尾的读者。

与本机资源通信的底层输入流(例如 FileInputStream 或从 URL 获取的流)必须关闭以释放这些本机资源。

对于 Writer 来说,不同之处在于 close() 通常调用 flush()。但随后,您可以直接调用flush()

You do not need to close a BufferedReader or InputStreamReader 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 that close() usually calls flush(). But then, you can call flush() directly.

停滞 2024-09-16 06:27:13

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.

情深已缘浅 2024-09-16 06:27:13

就我个人而言,我会通过更改方法的签名以要求传入 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.

好倦 2024-09-16 06:27:13

我会尝试覆盖 close 方法:

BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)){
    public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
        }
    }
};
// rest of your code

有点疯狂,但它应该可以工作。不过,我不确定这是最好的方法。

我觉得这个问题很有趣,希望有专家给出意见。

What I'd try would be overriding the close method:

BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)){
    public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
        }
    }
};
// rest of your code

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.

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