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));
            }
        }
    }

 

一般性總結:一般資源的打開順序和關閉順序相反


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM