这是我的代码:
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(new FileInputStream(TRUSTSTORE_FILE),
TRUSTSTORE_PASSWORD.toCharArray()); //sonarqube issue
哪个输入流最适合完成此任务?
这是完整的错误:
This method creates and uses a java.io.FileInputStream or java.io.FileOutputStream object. Unfortunately both of these classes implement a finalize method, which means that objects created will likely hang around until a full garbage collection occurs, which will leave excessive garbage on the heap for longer, and potentially much longer than expected.
我真的需要切换到:
InputStream is = java.nio.file.Files.newInputStream(myfile.toPath());
我对这个不太满意。
最佳答案
将其包装在 try-with-resources 中,以便它将在 block 末尾关闭。错误消息提示该文件可能打开了很长时间。
try (InputStream in = new FileInputStream(TRUSTSTORE_FILE)) {
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(in, TRUSTSTORE_PASSWORD.toCharArray());
} // Automatically closes in.
这会释放系统资源(文件句柄)并允许其他人覆盖信任库文件。
关于java - Sonarqube - 方法使用 FileInputStream 构造函数时出现性能问题,有哪些更好的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64209115/