diff --git a/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java b/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java index bddef4a0942d..49b33e9b1e59 100644 --- a/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java +++ b/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java @@ -368,10 +368,9 @@ private String generatePrivatePemKey(KeyPair keyPair) throws IOException { private String generatePemKey(String type, byte[] content) throws IOException { PemObject pemObject = new PemObject(type, content); StringWriter str = new StringWriter(); - JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str); - jcaPEMWriter.writeObject(pemObject); - jcaPEMWriter.close(); - str.close(); + try (JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str)) { + jcaPEMWriter.writeObject(pemObject); + } return str.toString(); } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java index ab2c1bd3fec0..ffc9738c5c04 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java @@ -47,11 +47,8 @@ public byte[] compress(byte[] payloadByteArr) throws RpcException { } ByteArrayOutputStream out = new ByteArrayOutputStream(); - BZip2CompressorOutputStream cos; - try { - cos = new BZip2CompressorOutputStream(out); + try (BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(out)) { cos.write(payloadByteArr); - cos.close(); } catch (Exception e) { throw new IllegalStateException(e); } @@ -75,9 +72,9 @@ public byte[] decompress(byte[] payloadByteArr) { } ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr); - try { - BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in); + try (ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr); + BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in)) { + byte[] buffer = new byte[2048]; int n; while ((n = unZip.read(buffer)) >= 0) {