是否可以在创建 InputSource 后立即关闭 InputStream?
Can I close InputStream right after creating InputSource?
我有一个 InputStream
,我正在将其转换为 InputSource
到 运行 几个 Xpath 查询。
我可以在创建 InputSource
后立即关闭 InputStream
还是仅在我不再需要 InputSource
时关闭?
这是我的代码:
InputStream stream = multipartfile.getInputStream()
InputSource source = new InputSource(stream)
stream.close() //here
Node root = (Node) xpath.evaluate("/", source, XPathConstants.NODE);
stream.close() //Or here?
这叫做Decorator pattern
。简短的回答是 否 ,您不应该在使用源对象之前关闭 InputStream
,因为它正在 InputSource
.[=18= 的功能中使用]
如果我们看一下装饰器模式的例子,我们就会明白为什么我们不应该改变作为构造函数参数传递的对象:
abstract class WindowDecorator implements Window {
protected Window windowToBeDecorated; // the Window being decorated
public WindowDecorator (Window windowToBeDecorated) {
this.windowToBeDecorated = windowToBeDecorated;
}
public void draw() {
windowToBeDecorated.draw(); //Delegation
}
public String getDescription() {
return windowToBeDecorated.getDescription(); //Delegation
}
}
在此示例中,您可以看到 windowToBeDecorated
作为对象在 draw()
或 getDescription()
方法中使用。
您可以查看有关装饰器模式的更多信息here。希望清楚。
您可以使用 try-with-resources 语句,这样可以确保在语句结束时关闭每个资源。
在这里你可以找到一个很好的例子:https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
你不能在解析器完成工作之前关闭InputStream
,否则它无法读取它。但是如果你看一下 InputSource
的 JavaDoc,甚至不需要显式关闭流,因为解析器应该关闭它:
An InputSource object belongs to the application: the SAX parser shall never modify it in any way (it may modify a copy if necessary). However, standard processing of both byte and character streams is to close them on as part of end-of-parse cleanup, so applications should not attempt to re-use such streams after they have been handed to a parser.
但是如果你想确保它是关闭的,你应该在解析器完成它的工作之后再做。实际上,您应该在 finally
块中执行此操作,以确保即使发生异常也能完成,或者您只需使用 try-with-resources 块即可适当地自动关闭它,例如:
try (InputStream stream = multipartfile.getInputStream()) {
InputSource source = new InputSource(stream)
Node root = (Node) xpath.evaluate("/", source, XPathConstants.NODE);
}
我有一个 InputStream
,我正在将其转换为 InputSource
到 运行 几个 Xpath 查询。
我可以在创建 InputSource
后立即关闭 InputStream
还是仅在我不再需要 InputSource
时关闭?
这是我的代码:
InputStream stream = multipartfile.getInputStream()
InputSource source = new InputSource(stream)
stream.close() //here
Node root = (Node) xpath.evaluate("/", source, XPathConstants.NODE);
stream.close() //Or here?
这叫做Decorator pattern
。简短的回答是 否 ,您不应该在使用源对象之前关闭 InputStream
,因为它正在 InputSource
.[=18= 的功能中使用]
如果我们看一下装饰器模式的例子,我们就会明白为什么我们不应该改变作为构造函数参数传递的对象:
abstract class WindowDecorator implements Window {
protected Window windowToBeDecorated; // the Window being decorated
public WindowDecorator (Window windowToBeDecorated) {
this.windowToBeDecorated = windowToBeDecorated;
}
public void draw() {
windowToBeDecorated.draw(); //Delegation
}
public String getDescription() {
return windowToBeDecorated.getDescription(); //Delegation
}
}
在此示例中,您可以看到 windowToBeDecorated
作为对象在 draw()
或 getDescription()
方法中使用。
您可以查看有关装饰器模式的更多信息here。希望清楚。
您可以使用 try-with-resources 语句,这样可以确保在语句结束时关闭每个资源。 在这里你可以找到一个很好的例子:https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
你不能在解析器完成工作之前关闭InputStream
,否则它无法读取它。但是如果你看一下 InputSource
的 JavaDoc,甚至不需要显式关闭流,因为解析器应该关闭它:
An InputSource object belongs to the application: the SAX parser shall never modify it in any way (it may modify a copy if necessary). However, standard processing of both byte and character streams is to close them on as part of end-of-parse cleanup, so applications should not attempt to re-use such streams after they have been handed to a parser.
但是如果你想确保它是关闭的,你应该在解析器完成它的工作之后再做。实际上,您应该在 finally
块中执行此操作,以确保即使发生异常也能完成,或者您只需使用 try-with-resources 块即可适当地自动关闭它,例如:
try (InputStream stream = multipartfile.getInputStream()) {
InputSource source = new InputSource(stream)
Node root = (Node) xpath.evaluate("/", source, XPathConstants.NODE);
}