先看出问题的一段代码
public void encode(String xxxPath, String thumbTmpPath, String imageType)
{
LOGGER.info("encode image, xxxPath={}, thumbTmpPath={}, imageType={}",
safeVideoPath,
thumbTmpPath,
imageType);
safeVideoPath = XxxUtils.trimPrefixDir(xxxPath);
InputStream srcInputStream = null;
FileOutputStream thumbOutStream = null;
BufferedImage srcBufImg;
BufferedImage result;
try
{
srcInputStream = getByteArrayInpuStreamByXxx(xxxPath);
thumbOutStream = new FileOutputStream(thumbTmpPath);
if (srcInputStream != null && thumbOutStream != null)
{
srcBufImg = ImageIO.read(srcInputStream);
result = new BufferedImage(srcBufImg.getWidth(), srcBufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = result.createGraphics();
graphics2D.drawImage(srcBufImg, 0, 0, null);
ImageIO.write(result, imageType, thumbOutStream);
}
}
catch (IOException e)
{
LOGGER.info("IO Error, {}", ExceptionUtils.getStackTrace(e));
}
finally
{
try
{
IOUtils.closeQuietly(srcInputStream);
}
catch (Exception e)
{
LOGGER.error("fail to close srcInputStream, {}", ExceptionUtils.getStackTrace(e));
}
try
{
IOUtils.closeQuietly(thumbOutStream);
}
catch (Exception e)
{
LOGGER.error("fail to close thumbOutStream, {}", ExceptionUtils.getStackTrace(e));
}
}
}
findbugs报出问题
XXXOOO.java:992, OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE, Priority: Normal
OBL: com.xxx1.xxx2.xxx3.XXXOOO.encode(String, String, String) may fail to clean up java.io.OutputStream on checked exception
This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation.
In general, if a method opens a stream or other resource, the method should use a try/finally block to ensure that the stream or resource is cleaned up before the method returns.
This bug pattern is essentially the same as the OS_OPEN_STREAM and ODR_OPEN_DATABASE_RESOURCE bug patterns, but is based on a different (and hopefully better) static analysis technique. We are interested is getting feedback about the usefulness of this bug pattern. To send feedback, either:
send email to findbugs@cs.umd.edu
file a bug report: http://findbugs.sourceforge.net/reportingBugs.html
In particular, the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us.
See Weimer and Necula, Finding and Preventing Run-Time Error Handling Mistakes, for a description of the analysis technique.
报错代码:thumbOutStream = new FileOutputStream(thumbTmpPath);
原因分析:由于thumbOutStream依赖于流srcInputStream,如果srcInputStream关闭失败,就会导致thumbOutStream也关闭失败,所以恰当的做法是先关闭依赖流,再关闭被依赖的流。
修改后的代码如下,将关闭流的顺序反过来即可
public void encode(String safeVideoPath, String thumbTmpPath, String imageType) { LOGGER.info("encode image, xxxPath={}, thumbTmpPath={}, imageType={}", xxxPath, thumbTmpPath, imageType); safeVideoPath = XxxUtil.trimPrefixDir(xxxPath); InputStream srcInputStream = null; FileOutputStream thumbOutStream = null; BufferedImage srcBufImg; BufferedImage result; try { srcInputStream = getByteArrayInpuStreamByXxx(xxxPath); thumbOutStream = new FileOutputStream(thumbTmpPath); if (srcInputStream != null && thumbOutStream != null) { srcBufImg = ImageIO.read(srcInputStream); result = new BufferedImage(srcBufImg.getWidth(), srcBufImg.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = result.createGraphics(); graphics2D.drawImage(srcBufImg, 0, 0, null); ImageIO.write(result, imageType, thumbOutStream); } } catch (IOException e) { LOGGER.info("IO Error, {}", ExceptionUtils.getStackTrace(e)); }finally { try { IOUtils.closeQuietly(thumbOutStream); } catch (Exception e) { LOGGER.error("fail to close thumbOutStream, {}", ExceptionUtils.getStackTrace(e)); } try { IOUtils.closeQuietly(srcInputStream); } catch (Exception e) { LOGGER.error("fail to close srcInputStream, {}", ExceptionUtils.getStackTrace(e)); } } }
一般性总结:一般资源的打开顺序和关闭顺序相反