Pre-check

  • [x] I am sure that all the content I provide is in English.

Search before asking

  • [x] I had searched in the issues and found no similar issues.

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

Dubbo 3.3.1 , JDK 17, mac 15.3.2

Steps to reproduce this issue

   public byte[] compress(byte[] payloadByteArr) throws RpcException {
        if (null == payloadByteArr || 0 == payloadByteArr.length) {
            return new byte[0];
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BZip2CompressorOutputStream cos;
        try {
            cos = new BZip2CompressorOutputStream(out);
            cos.write(payloadByteArr);
            cos.close();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }

        return out.toByteArray();
    }

If the cos.write() operation encounters an IO exception, the cos.close() method will not execute, which may result in incorrect resource release. Should execute close() in finally {} or use try-with-resources

What you expected to happen

Using try-with-resources to manipulate BZip2CompressorOutputStream

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • [x] Yes I am willing to submit a pull request on my own!

Code of Conduct

Comment From: oxsean

@paxxie2 BZip2CompressorOutputStream does not hold any file(or io) handle here, and ByteArrayOutputStream itself will be release by gc. Therefore, even if it is not explicitly closed here, it should not cause any issue.