<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>

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

        时间:2024-05-09
        • <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.

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

                  上一篇:SonarQube:(改为获取特定异常子类型的列表) 下一篇:如果 JUnit 覆盖率低于某个阈值,如何使 Maven 构建失败

                  相关文章

                  1. <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>

                    <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'>