先看出问题的一段代码

  1. public void encode(String xxxPath, String thumbTmpPath, String imageType)
  2. {
  3. LOGGER.info("encode image, xxxPath={}, thumbTmpPath={}, imageType={}",
  4. safeVideoPath,
  5. thumbTmpPath,
  6. imageType);
  7. safeVideoPath = XxxUtils.trimPrefixDir(xxxPath);
  8. InputStream srcInputStream = null;
  9. FileOutputStream thumbOutStream = null;
  10. BufferedImage srcBufImg;
  11. BufferedImage result;
  12. try
  13. {
  14. srcInputStream = getByteArrayInpuStreamByXxx(xxxPath);
  15. thumbOutStream = new FileOutputStream(thumbTmpPath);
  16. if (srcInputStream != null && thumbOutStream != null)
  17. {
  18. srcBufImg = ImageIO.read(srcInputStream);
  19. result = new BufferedImage(srcBufImg.getWidth(), srcBufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
  20. Graphics2D graphics2D = result.createGraphics();
  21. graphics2D.drawImage(srcBufImg, 0, 0, null);
  22. ImageIO.write(result, imageType, thumbOutStream);
  23. }
  24. }
  25. catch (IOException e)
  26. {
  27. LOGGER.info("IO Error, {}", ExceptionUtils.getStackTrace(e));
  28. }
  29. finally
  30. {
  31. try
  32. {
  33. IOUtils.closeQuietly(srcInputStream);
  34. }
  35. catch (Exception e)
  36. {
  37. LOGGER.error("fail to close srcInputStream, {}", ExceptionUtils.getStackTrace(e));
  38. }
  39. try
  40. {
  41. IOUtils.closeQuietly(thumbOutStream);
  42. }
  43. catch (Exception e)
  44. {
  45. LOGGER.error("fail to close thumbOutStream, {}", ExceptionUtils.getStackTrace(e));
  46. }
  47. }
  48. }

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也关闭失败,所以恰当的做法是先关闭依赖流,再关闭被依赖的流。

修改后的代码如下,将关闭流的顺序反过来即可

  1. public void encode(String safeVideoPath, String thumbTmpPath, String imageType)
  2. {
  3. LOGGER.info("encode image, xxxPath={}, thumbTmpPath={}, imageType={}",
  4. xxxPath,
  5. thumbTmpPath,
  6. imageType);
  7. safeVideoPath = XxxUtil.trimPrefixDir(xxxPath);
  8. InputStream srcInputStream = null;
  9. FileOutputStream thumbOutStream = null;
  10. BufferedImage srcBufImg;
  11. BufferedImage result;
  12. try
  13. {
  14. srcInputStream = getByteArrayInpuStreamByXxx(xxxPath);
  15. thumbOutStream = new FileOutputStream(thumbTmpPath);
  16. if (srcInputStream != null && thumbOutStream != null)
  17. {
  18. srcBufImg = ImageIO.read(srcInputStream);
  19. result = new BufferedImage(srcBufImg.getWidth(), srcBufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
  20. Graphics2D graphics2D = result.createGraphics();
  21. graphics2D.drawImage(srcBufImg, 0, 0, null);
  22. ImageIO.write(result, imageType, thumbOutStream);
  23. }
  24. }
  25. catch (IOException e)
  26. {
  27. LOGGER.info("IO Error, {}", ExceptionUtils.getStackTrace(e));
  28. }finally
  29. {
  30. try
  31. {
  32. IOUtils.closeQuietly(thumbOutStream);
  33. }
  34. catch (Exception e)
  35. {
  36. LOGGER.error("fail to close thumbOutStream, {}", ExceptionUtils.getStackTrace(e));
  37. }
  38. try
  39. {
  40. IOUtils.closeQuietly(srcInputStream);
  41. }
  42. catch (Exception e)
  43. {
  44. LOGGER.error("fail to close srcInputStream, {}", ExceptionUtils.getStackTrace(e));
  45. }
  46. }
  47. }

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

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. 7.7 Varnishadm命令

  2. 十五、React:简单点餐实例:知识点,html解析写法

    一.功能 从首页列表点进去,转到详情页 列表.详情从Api获取 Api列表:http://a.itying.com/api/productlist 详情:http://a.itying.com/api ...

  3. Problem C Updating a Dictionary

    Problem C     Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...

  4. python中pip的安装问题

    当系统中同时存在python2.python3的时候,在进行pip下载的时总出现不知道用哪个 情况,此时就需要这样做: python2 -m pip install numpy //使用pip给pyt ...

  5. VS2013+HALCON13

    HALCON安装与配置(VS2013+HALCON13) 2017-06-23 16:08:25 坚强的羊脂球 阅读数 4574更多 分类专栏: HALCON   配置主要分为三部分: 1)VS调用H ...

  6. bzoj 3522: [Poi2014]Hotel

    呵呵,一开始天真的我以为求个 西格玛 C(??,3)就好了.. (题解:比枚举2个数的再多一个,,一样搞) #include <bits/stdc++.h> #define LL long ...

  7. 09.swoole学习笔记--进程事件

    <?php //进程数组 $workers=[]; //创建进程的数据量 $worker_num=; //创建启动进程 ;$i<$worker_num;$i++){ //创建单独新进程 $ ...

  8. zoj 1483 划分类DP

    还是看了little_w大神写的才知道怎么写,看完发现自己题意也理解错了,里面有个neighboring,意思就是你指定任务的时候指定的是原序列中连续的一段 然后就是怎么DP了,新学了个很好的dp模型 ...

  9. opencv.js双边滤波 磨皮处理

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...

  10. yagmail四行代码发送邮件

    yagmail四行代码发送邮件 import yagmail # 链接邮箱服务器 yag = yagmail.SMTP(user="xxxx@163.com", password= ...