【问题标题】:Stream closed exception in javajava中的流关闭异常
【发布时间】:2011-02-10 03:47:41
【问题描述】:

请看看我做了什么

private InputSource getContent(String fName) throws SAXException, IOException, ServiceException {
        // Some code here
        if(currentNodeRef != null)
        {
            ContentReader reader = contentService.getReader(currentNodeRef,ContentModel.PROP_CONTENT);

            InputStream inputStream = null;
             try 
             {  
                    inputStream = new BufferedInputStream(reader.getContentInputStream(),16384);

                    return new InputSource(inputStream);
             }
             finally
             {
                 if(inputStream!=null)
                    try 
                    {
                        inputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             }
        }
        return new InputSource();
}

在我的 parseDocument 方法中,我调用了上述方法。

parseDocRoot(getContent(fName),path);

在 parseDocRoot 中

public  void parseDocRoot (InputSource ins, String path) throws SAXException, IOException,
  ParserConfigurationException, ServiceException
  {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setEntityResolver(new EntityResolver() {
                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {
                     return new InputSource(new ByteArrayInputStream(new byte[0])); 
                }
            });
        Document doc = builder.parse(ins);
        NodeList list = doc.getChildNodes();
        parseDocument(list,path);
  }

我得到一个错误说 Stream Not closed 并且在调试上面的代码时我发现错误就行了

Document doc = builder.parse(ins);

请帮我找到解决办法。

【问题讨论】:

  • (第一个)答案很可能是正确的,但通常您最好提供完整的堆栈跟踪而不是错误描述;它使事情变得更容易并避免混淆。

标签: java exception stream


【解决方案1】:

我认为错误是流 已关闭。原因是你有一个 finally 块:

         try 
         {  
                inputStream = new BufferedInputStream(reader.getContentInputStream(),16384);

                return new InputSource(inputStream);
         }
         finally
         {
             if(inputStream!=null)
                try 
                {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
         }

关闭 finally 块中的流。此代码在从方法返回之前立即执行。重要的是,直到方法返回并由parse 方法处理后,InputStream 才会使用

您应该将 finally 块放在整个代码块周围——当您真正完成时关闭流。最简单的方法是内联您的getContent 方法并在调用parse 之后放置finally 块。完成此操作后,您可能会想出一种封装该逻辑的方法,但这会有点棘手,因为您肯定需要保留 InputStream 的句柄,直到完成解析才能关闭它.

另一个更简单的选择是让getContent 返回一个Document,而不是简单地在该方法中移动parseDocRoot(getContent(fName),path);

【讨论】:

  • 谢谢柯克。我已经尝试过了并且正在工作。感谢您的回答。将按照您所说的进行更改。
  • 这个答案是正确的,但是您构建代码的方式可能并不理想,因为我猜现在您根本没有关闭流。我建议不要使用单独的方法来返回 InputSource。我认为尝试以相同的方法打开和关闭流是一个好习惯。 (显然你可以调用其他方法来处理流)。
  • @MeBigFatGuy,我相信这就是我的建议。特别是我最后的建议是简单地返回一个Document——这个想法是让getContent负责打开和关闭流。
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 2013-05-28
  • 2014-07-20
相关资源
最近更新 更多