简体   繁体   English

java8中的ByteArrayOutputStream异常处理

[英]ByteArrayOutputStream exception handling in java8

private void logSoapMessage(SOAPMessage soapMessage, String type) {
        ByteArrayOutputStream bout = null;
        try {
            bout = new ByteArrayOutputStream();
            soapMessage.writeTo(bout);
            logger.info("The " + type + " is " + bout.toString("UTF-8"));
        } catch (IOException e) {
            logger.debug("Exception while logging soap message " + e);
        }
    }

Is above code can give resource leak error?上面的代码是否会出现资源泄漏错误?

Is above code can give resource leak error?上面的代码是否会出现资源泄漏错误?

No it isn't / doesn't.不,它不是/没有。

A ByteArrayOutputStream doesn't have any resources 1 that can leak. ByteArrayOutputStream没有任何可能泄漏的资源1

However, an unsophisticated static checker might mistakenly flag your code as a potential resource leak.但是,简单的 static 检查器可能会错误地将您的代码标记为潜在的资源泄漏。 You could either mark this as a "false positive" in the appropriate way, or just close the stream anyway.您可以以适当的方式将其标记为“误报”,或者无论如何都关闭 stream。 The following is the best way to do the latter for Java 7 and later:以下是对 Java 7 及更高版本执行后者的最佳方法:

    try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
        soapMessage.writeTo(bout);
        logger.info("The " + type + " is " + bout.toString("UTF-8"));
    } catch (IOException e) {
        logger.debug("Exception while logging soap message " + e);
    }

1 - Resources refers to things like file descriptors that are obtained from the operating system, and are only available in limited numbers. 1 - 资源是指从操作系统获得的文件描述符之类的东西,并且数量有限。 If these resources are not managed properly by a Java application, there is a risk that the application will run out of them.如果 Java 应用程序未正确管理这些资源,则存在应用程序将用完它们的风险。 It is conventional for an object that holds a resource to implement AutoClosable and provide a close() method that releases the resources.对于持有资源的 object 来说,传统的做法是实现AutoClosable并提供释放资源的close()方法。

Its because you're not closing the resource when you're done with it.这是因为您在完成资源后没有关闭它。

Java 7 introduced the try-with-resource function: Java 7 介绍了try-with-resource function:

private void logSoapMessage(SOAPMessage soapMessage, String type) {
  try (ByteArrayOutputStream bout = new ByteArrayOutputStream()){
    soapMessage.writeTo(bout);
    logger.info("The " + type + " is " + bout.toString("UTF-8"));
  } catch (IOException e) {
    logger.debug("Exception while logging soap message " + e);
  }
}

Which will automatically close the ByteArrayOutputStream even if it throws an exception.即使抛出异常,它也会自动关闭ByteArrayOutputStream

The old way prior to java 7 you'd have to write it this way: java 7 之前的旧方法你必须这样写:

private void logSoapMessage(SOAPMessage soapMessage, String type) {
  ByteArrayOutputStream bout = null;
  try {
    bout = new ByteArrayOutputStream();
    soapMessage.writeTo(bout);
    logger.info("The " + type + " is " + bout.toString("UTF-8"));
  } catch (IOException e) {
    logger.debug("Exception while logging soap message " + e);
  } finally {
    if (bout != null) bout.close();
  }
}

To make sure the resource was closed prior to exiting the method and not get a resource leak.确保在退出方法之前资源已关闭并且不会导致资源泄漏。

Here is some info about the try-with-resource: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html以下是有关 try-with-resource 的一些信息: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.ZFC35FDC70D5FC69D2639883A82EZA7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM