如何正确地将 InputStream 传递给另一个构造函数? (Java)
How to properly pass InputStream to another constructor? (Java)
在下面的代码中,我尝试访问我的另一个采用 InputStream 的构造函数...
但是我需要以某种方式关闭此流以避免资源泄漏。如果我尝试使用 try catch,它会抱怨构造函数调用不是第一条语句。有什么方法可以不带任何风险地传递这个InputStream吗?
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
您需要使此 class 实现 AutoClosable
并确保在 try-with-resource 中使用它:
public class Input extends SomeClass implements AutoCloseable {
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
@Override
public void close() {
someMethodThatClosesInnerResource();
}
}
然后您可以像这样使用该对象:
try (Input input = new Input(source)) {
...
}
如果您担心超级构造函数会抛出异常,那么您可能想要添加一个构建器方法来防止早期异常:
public class Input extends SomeClass implements AutoCloseable {
public static Input createInput(File source) throws Exception {
FileInputStream inputstream = new FileInputStream(source);
try {
return new Input(inputstream);
} catch (Exception e) {
inputstream.close();
throw e;
}
}
private Input(FileInputStream source) {
this(source);
}
@Override
public void close() {
someMethodThatClosesInnerResource();
}
}
您仍然应该将其用作尝试资源:
try (Input input = Input.createInput(source)) {
...
}
请注意,在构建器方法中,您必须防止所有异常...这不是很好的做法,因为现在您必须让该方法抛出一般异常...或使用实用程序静默抛出。您可以通过不让构造函数创建 FileInputStream
来避免这个问题。相反,只需将 FileInputStream
注入您的 class 并在 try-with-resource 中创建它。这将安全地允许在 ctor 异常的情况下关闭两个对象:
try (
FileInputStream stream = new FileInputStream(source);
Input input = new Input(stream)
) {
...
}
在下面的代码中,我尝试访问我的另一个采用 InputStream 的构造函数... 但是我需要以某种方式关闭此流以避免资源泄漏。如果我尝试使用 try catch,它会抱怨构造函数调用不是第一条语句。有什么方法可以不带任何风险地传递这个InputStream吗?
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
您需要使此 class 实现 AutoClosable
并确保在 try-with-resource 中使用它:
public class Input extends SomeClass implements AutoCloseable {
public Input(File source) throws FileNotFoundException {
this(new FileInputStream(source));
}
@Override
public void close() {
someMethodThatClosesInnerResource();
}
}
然后您可以像这样使用该对象:
try (Input input = new Input(source)) {
...
}
如果您担心超级构造函数会抛出异常,那么您可能想要添加一个构建器方法来防止早期异常:
public class Input extends SomeClass implements AutoCloseable {
public static Input createInput(File source) throws Exception {
FileInputStream inputstream = new FileInputStream(source);
try {
return new Input(inputstream);
} catch (Exception e) {
inputstream.close();
throw e;
}
}
private Input(FileInputStream source) {
this(source);
}
@Override
public void close() {
someMethodThatClosesInnerResource();
}
}
您仍然应该将其用作尝试资源:
try (Input input = Input.createInput(source)) {
...
}
请注意,在构建器方法中,您必须防止所有异常...这不是很好的做法,因为现在您必须让该方法抛出一般异常...或使用实用程序静默抛出。您可以通过不让构造函数创建 FileInputStream
来避免这个问题。相反,只需将 FileInputStream
注入您的 class 并在 try-with-resource 中创建它。这将安全地允许在 ctor 异常的情况下关闭两个对象:
try (
FileInputStream stream = new FileInputStream(source);
Input input = new Input(stream)
) {
...
}