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));
}
}
}
一般性总结:一般资源的打开顺序和关闭顺序相反
findbugs报OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE的修改实例的更多相关文章
- Java反射机制可以动态修改实例中final修饰的成员变量吗?
问题:Java反射机制可以动态修改实例中final修饰的成员变量吗? 回答是分两种情况的. 1. 当final修饰的成员变量在定义的时候就初始化了值,那么java反射机制就已经不能动态修改它的值了. ...
- ORACLE_修改实例的内存大小
注:本文来源于:星火spark <Oracle的实例占用内存调整> ORACLE_修改实例的内存大小 一:修改oracle数据库实例内存大小脚本 ---- 1.操作 (oracle使用内 ...
- 【PP系列】SAP 取消报工后修改日期
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[PP系列]SAP 取消报工后修改日期 前言 ...
- python踩坑系列——报错后修改了.py文件,但是依然报错
一开始.py文件中的函数名大小写错了,但是在终端是对的,报错: 'module' object has no attribute '某函数名' 后来就去修改.py文件.结果重新import该.py文件 ...
- FindBugs报错
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- openstack私有云布署实践【18 修改实例DHCP服务的DNS IP】
某天,由于Linux服务器默认没有DNS缓存功能,每次服务器每访问一个http域名链接时,都会触发一次DNS域名解析查询,降低了调用API接口的时延,所以我司后续启用的内网的dnsmasq DNS服务 ...
- Dynamics CRM 打开数据加密报错及修改用户邮件保存报错的解决方法
在项目里会碰到在修改用户的电子邮件时报错的问题 然后跑到数据管理里打开数据加密又是报错 解决上述问题只需要做下数据库的更改即可,把标志位置1即可,记得要重启下IIS才能生效 SELECT [Colum ...
- JPA报错问题修改小结
项目中在使用线程跑定时任务时,遇到报错,"Could not open JPA EntityManager for transaction Caused by: org.hibernate. ...
- RocketMQ_问题_启动报错,修改堆内存大小
1.启动broker报错 虚拟机内存小,导致虚拟机中的JVM内存小,进而在启动broker时分配JVM内存遇到问题 查询网上得知,查看/usr/local/rocketmq-all-4.3.0/dis ...
随机推荐
- mysql 索引入门
创建索引的语法结构:
- B. Yet Another Crosses Problem
B. Yet Another Crosses Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- NRF51822和NRF52832的主要区别
对于NRF51822和NRF52832的选择性相信大家也是非常困惑的,哪个性价比高?下面为大家讲下NRF51822和NRF52832的一个区别,让大家能够更好的快速选型加快研发产品进度! 主要分为 ...
- JS - 对话框
1,<span onclick="return confirmAct();">执行操作</span> 2, function confirmAct(){ i ...
- jQuery通过name获取值
使用jQuery获取name="day"的input对象: 方法1 var dayObj=$('input[name="day"]'); for(int i=0 ...
- servlet中urlpatterns注意事项
在servlet中, @WebServlet(urlPatterns="/newsAdd")接收 resp.sendRedirect("/wedding/houtai/N ...
- unicode字符等价探究
Demobaidu.com(\uff41)能跳转到baidu.combаidu.com(\u0430)不能跳转到baidu.com,被认为成一个其他域名 等价原因两个不同编码的Unicode字符之间可 ...
- Flink 复杂事物处理
简介 FlinkCEP是在Flink之上实现的复杂事件处理(CEP)库. 它允许你在无界的事件流中检测事件模式,让你有机会掌握数据中重要的事项. Flink CEP 首先需要用户创建定义一个个patt ...
- CSU-ACM2020寒假集训比赛2
A - Messenger Simulator CodeForces - 1288E 两种解法,我选择了第二种 mn很好求,联系过就是1,没联系过就是初始位置 第一种:统计同一个人两次联系之间的出现的 ...
- ESP8266 SDK开发: 外设篇-串口
串口分布 串口切换说明 1.默认所有的数据都使用串口0输出 官方提供了函数可以选择printf利用哪一个串口输出 配置printf使用串口1打印输出,波特率115200 (注:这样配置对于调试程序很有 ...