findbugs报OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE的修改实例
先看出问题的一段代码
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));
}
}
}
一般性总结:一般资源的打开顺序和关闭顺序相反
最新文章
- SharePoint 2013 图文开发系列之代码定义列表
- Oracle开发常用函数
- Android性能之启动时间篇
- HTML实体字符转化为HTML标签
- IOS SizeClasses 详解
- Java for LeetCode 201 Bitwise AND of Numbers Range
- javascript 变量解析
- csu 10月 月赛 D 题 CX and girls
- swift3.0 hello swift(1)
- Ambari-部署文档
- 读书笔记 之《Thinking in Java》(对象、集合)
- C#之DataTable转List与List转Datatable
- CentOS Linux上安装Oracle11g笔记
- 使用nio遍历文件夹
- Mysql 根据一个表数据更新另外一个表
- Python 入门基础18 --re模块+内存管理
- python3进行汉字和unicode码的转换
- BZOJ.1028.[JSOI2007]麻将(贪心)
- 20135234mqy 实验三:敏捷开发与XP实践
- select设置高度的兼容问题