先看出问题的一段代码

    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的修改实例的更多相关文章

  1. Java反射机制可以动态修改实例中final修饰的成员变量吗?

    问题:Java反射机制可以动态修改实例中final修饰的成员变量吗? 回答是分两种情况的. 1. 当final修饰的成员变量在定义的时候就初始化了值,那么java反射机制就已经不能动态修改它的值了. ...

  2. ORACLE_修改实例的内存大小

    注:本文来源于:星火spark  <Oracle的实例占用内存调整> ORACLE_修改实例的内存大小 一:修改oracle数据库实例内存大小脚本 ---- 1.操作 (oracle使用内 ...

  3. 【PP系列】SAP 取消报工后修改日期

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[PP系列]SAP 取消报工后修改日期   前言 ...

  4. python踩坑系列——报错后修改了.py文件,但是依然报错

    一开始.py文件中的函数名大小写错了,但是在终端是对的,报错: 'module' object has no attribute '某函数名' 后来就去修改.py文件.结果重新import该.py文件 ...

  5. FindBugs报错

    FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...

  6. openstack私有云布署实践【18 修改实例DHCP服务的DNS IP】

    某天,由于Linux服务器默认没有DNS缓存功能,每次服务器每访问一个http域名链接时,都会触发一次DNS域名解析查询,降低了调用API接口的时延,所以我司后续启用的内网的dnsmasq DNS服务 ...

  7. Dynamics CRM 打开数据加密报错及修改用户邮件保存报错的解决方法

    在项目里会碰到在修改用户的电子邮件时报错的问题 然后跑到数据管理里打开数据加密又是报错 解决上述问题只需要做下数据库的更改即可,把标志位置1即可,记得要重启下IIS才能生效 SELECT [Colum ...

  8. JPA报错问题修改小结

    项目中在使用线程跑定时任务时,遇到报错,"Could not open JPA EntityManager for transaction Caused by: org.hibernate. ...

  9. RocketMQ_问题_启动报错,修改堆内存大小

    1.启动broker报错 虚拟机内存小,导致虚拟机中的JVM内存小,进而在启动broker时分配JVM内存遇到问题 查询网上得知,查看/usr/local/rocketmq-all-4.3.0/dis ...

随机推荐

  1. 开源免费的安卓投屏工具-Scrcpy

    最近需要使用安卓投屏在桌面上操作,一开始使用Vysor,免费版画质无法直视,发现一个开源的工具,Scrcpy,貌似效果不错,但没有GUI,命令行安装,整起(Mac) 1.安装 homebrew: 通过 ...

  2. Centos 8下普通用户增加root权限

    问题:     解决: 重启Centos,使用root登陆:                    

  3. HihoCoder第十三周:最近公共祖先 一

    #1062 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 同城那样神奇,但这个网站仍然让小Ho乐在其中,但这是为什么呢? "为什么呢?& ...

  4. 153-PHP htmlentities函数

    <?php //定义一个HTML代码字符串 $str=<<<HTM <a href=#><b><i>到一个网址的链接</i>&l ...

  5. 087-把PHP数组中的元素按随机顺序重新排列shuffle

    <?php $arr=array(3,23,'A','f','123','hello'); //定义一个数组 echo '排序之前的数组信息:<br>'; print_r($arr) ...

  6. Python MySQL Where

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  7. Java高级特性——注解,这也许是最简单易懂的文章了

    最近,浪尖在做flink的项目时source和sink的绑定那块用到了注解,当然新版本1.6以后就变为server load的方式加载. 但是浪尖也是觉得很有毕业讲一下注解,毕竟高级免试也会问答的吧. ...

  8. css设置兄弟节点的样式(相邻的前一个节点)

    产品需求:想在鼠标移动到“移除”的时候,“1.产品匹配测试”添加下划线和更改字体颜色 需求分析:从需求可以看出使用 :hover 就可以解决的问题,但是在实践中发现兄弟选择器(+)不好使,(+)只能是 ...

  9. Promise 练习

    import { ModelClass } from "engine/injector/Injector"; import { Inject } from "core/i ...

  10. VNC连接桌面

    1.#yum -y install vnc *vnc-server* 2.修改VNCServer主配置文件 #vim /etc/sysconfig/vncservers 复制最后两行并去掉行首注释符, ...