先看出问题的一段代码

    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));
}
}
}

一般性总结:一般资源的打开顺序和关闭顺序相反

最新文章

  1. SharePoint 2013 图文开发系列之代码定义列表
  2. Oracle开发常用函数
  3. Android性能之启动时间篇
  4. HTML实体字符转化为HTML标签
  5. IOS SizeClasses 详解
  6. Java for LeetCode 201 Bitwise AND of Numbers Range
  7. javascript 变量解析
  8. csu 10月 月赛 D 题 CX and girls
  9. swift3.0 hello swift(1)
  10. Ambari-部署文档
  11. 读书笔记 之《Thinking in Java》(对象、集合)
  12. C#之DataTable转List与List转Datatable
  13. CentOS Linux上安装Oracle11g笔记
  14. 使用nio遍历文件夹
  15. Mysql 根据一个表数据更新另外一个表
  16. Python 入门基础18 --re模块+内存管理
  17. python3进行汉字和unicode码的转换
  18. BZOJ.1028.[JSOI2007]麻将(贪心)
  19. 20135234mqy 实验三:敏捷开发与XP实践
  20. select设置高度的兼容问题

热门文章

  1. JS: 子项可以来回交换的两个下拉列表
  2. class(二)--派生类的继承
  3. java分词技术(自动提取关键词,段落大意)hanlp
  4. H5页面,百度地图点击事件
  5. 博客已经转到www.vsyf.me/blog
  6. 设置gvim的字体大小
  7. 在 Windows 系统上安装 Jekyll
  8. HDU_4960 2014多校9 Another OCD Patient DP
  9. int, float, double 等转化为 string
  10. opencv运动物体识别