use*_*011 0 java exception inputstream sonarqube
我有这个方法:
private void unZipElementsTo(String inputZipFileName, String destPath) throws FileNotFoundException, IOException {
OutputStream out = null;
InputStream in = null;
ZipFile zf = null;
try {
zf = new ZipFile(inputZipFileName);
for (Enumeration<? extends ZipEntry> em = zf.entries(); em.hasMoreElements();) {
ZipEntry entry = em.nextElement();
String targetFile = destPath + FILE_SEPARATOR + entry.toString().replace("/", FILE_SEPARATOR);
File temp = new File(targetFile);
if (!temp.getParentFile().exists()) {
temp.getParentFile().mkdirs();
}
in = zf.getInputStream(entry);
out = new FileOutputStream(targetFile);
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
out.close();
in.close();
}
}
finally
{
if (out!=null) out.close();
if (zf!=null) zf.close();
if (in!=null) in.close();
}
}
Run Code Online (Sandbox Code Playgroud)
对于这种方法,声纳给我这个违规行为:
不好的做法 - 方法可能无法在异常时关闭流unZipElementsTo(String,String)可能无法关闭异常流
但是,我没有看到任何违规行为.也许,这只是假阳性?
那就对了.该OutputStream.close()方法本身可以抛出异常.如果发生这种情况,例如在finally{}块的第一行,则其他流将保持打开状态.
| 归档时间: |
|
| 查看次数: |
8061 次 |
| 最近记录: |