<bdo id='y1vCk'></bdo><ul id='y1vCk'></ul>
  • <i id='y1vCk'><tr id='y1vCk'><dt id='y1vCk'><q id='y1vCk'><span id='y1vCk'><b id='y1vCk'><form id='y1vCk'><ins id='y1vCk'></ins><ul id='y1vCk'></ul><sub id='y1vCk'></sub></form><legend id='y1vCk'></legend><bdo id='y1vCk'><pre id='y1vCk'><center id='y1vCk'></center></pre></bdo></b><th id='y1vCk'></th></span></q></dt></tr></i><div id='y1vCk'><tfoot id='y1vCk'></tfoot><dl id='y1vCk'><fieldset id='y1vCk'></fieldset></dl></div>

    <small id='y1vCk'></small><noframes id='y1vCk'>

    <legend id='y1vCk'><style id='y1vCk'><dir id='y1vCk'><q id='y1vCk'></q></dir></style></legend>
    1. <tfoot id='y1vCk'></tfoot>

        声纳违规:“方法可能无法在异常时关闭流"

        Sonar violation: quot;Method may fail to close stream on exceptionquot;(声纳违规:“方法可能无法在异常时关闭流)
        • <i id='3H4N6'><tr id='3H4N6'><dt id='3H4N6'><q id='3H4N6'><span id='3H4N6'><b id='3H4N6'><form id='3H4N6'><ins id='3H4N6'></ins><ul id='3H4N6'></ul><sub id='3H4N6'></sub></form><legend id='3H4N6'></legend><bdo id='3H4N6'><pre id='3H4N6'><center id='3H4N6'></center></pre></bdo></b><th id='3H4N6'></th></span></q></dt></tr></i><div id='3H4N6'><tfoot id='3H4N6'></tfoot><dl id='3H4N6'><fieldset id='3H4N6'></fieldset></dl></div>

            <legend id='3H4N6'><style id='3H4N6'><dir id='3H4N6'><q id='3H4N6'></q></dir></style></legend>
          • <tfoot id='3H4N6'></tfoot>
              <bdo id='3H4N6'></bdo><ul id='3H4N6'></ul>
                <tbody id='3H4N6'></tbody>

                  <small id='3H4N6'></small><noframes id='3H4N6'>

                1. 本文介绍了声纳违规:“方法可能无法在异常时关闭流"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这个方法:

                   private void unZipElementsTo(String inputZipFileName, String destPath) throws FileNotFoundException, IOException {
                  
                          OutputStream out = null;
                          InputStream in = null;
                          ZipFile zf = null;
                  
                          try {
                              zf = new ZipFile(inputZipFileName);
                  
                              for (Enumeration<? extends ZipEntry> em = zf.entries(); em.hasMoreElements();) {
                                  ZipEntry entry = em.nextElement();
                                  String targetFile = destPath + FILE_SEPARATOR + entry.toString().replace("/", FILE_SEPARATOR);
                                  File temp = new File(targetFile);
                  
                                  if (!temp.getParentFile().exists()) {
                                      temp.getParentFile().mkdirs();
                                  }
                  
                                  in = zf.getInputStream(entry);
                  
                                  out = new FileOutputStream(targetFile);
                                  byte[] buf = new byte[4096];
                                  int len;
                                  while ((len = in.read(buf)) > 0) {
                                      out.write(buf, 0, len);
                                  }
                                  out.flush();
                                  out.close();
                                  in.close();
                              }
                          }
                          finally
                          {
                              if (out!=null) out.close();
                              if (zf!=null) zf.close();
                              if (in!=null) in.close();
                          }
                      }
                  

                  对于这个方法,Sonar 给我这个违规:

                  For this method Sonar give me this Violation:

                  不好的做法 - 方法可能无法在异常时关闭流unZipElementsTo(String, String) 可能无法在异常时关闭流

                  Bad practice - Method may fail to close stream on exception unZipElementsTo(String, String) may fail to close stream on exception

                  但是,我没有看到任何违规行为.也许,它只是一个假阳性?

                  But, I don't see any violations there. Maybe, it is just a False-positive ?

                  推荐答案

                  没错.OutputStream.close() 方法本身可以抛出异常.如果发生这种情况,例如在 finally{} 块的第一行,其他流将保持打开状态.

                  That's right. The OutputStream.close() method can itself throw an exception. If this happens, e.g. on the 1st line of your finally{} block, then the other streams will be left open.

                  这篇关于声纳违规:“方法可能无法在异常时关闭流"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                  Security - Array is stored directly(安全性 - 数组直接存储)
                  SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                  Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                  How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                  automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)

                    • <i id='tKWq2'><tr id='tKWq2'><dt id='tKWq2'><q id='tKWq2'><span id='tKWq2'><b id='tKWq2'><form id='tKWq2'><ins id='tKWq2'></ins><ul id='tKWq2'></ul><sub id='tKWq2'></sub></form><legend id='tKWq2'></legend><bdo id='tKWq2'><pre id='tKWq2'><center id='tKWq2'></center></pre></bdo></b><th id='tKWq2'></th></span></q></dt></tr></i><div id='tKWq2'><tfoot id='tKWq2'></tfoot><dl id='tKWq2'><fieldset id='tKWq2'></fieldset></dl></div>
                      • <bdo id='tKWq2'></bdo><ul id='tKWq2'></ul>

                            <tbody id='tKWq2'></tbody>
                          <tfoot id='tKWq2'></tfoot>
                          1. <legend id='tKWq2'><style id='tKWq2'><dir id='tKWq2'><q id='tKWq2'></q></dir></style></legend>

                            <small id='tKWq2'></small><noframes id='tKWq2'>