zoukankan      html  css  js  c++  java
  • 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));
                }
            }
        }

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

  • 相关阅读:
    jmeter录制
    Jmeter之逻辑控制器
    Jmeter关联
    Jmeter之HTTP请求默认值
    shell一文入门通
    Linux系统编程——基础命令总结
    前端专业方向的尽头
    锤子,技术与交互体验细节
    学习汇总 2019-12-2
    不容错过的 Babel7 知识
  • 原文地址:https://www.cnblogs.com/xsl-thumb-rfcs/p/9955335.html
Copyright © 2011-2022 走看看