diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java index d3c615b75532..cd252be3e873 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java @@ -22,7 +22,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -786,11 +785,9 @@ public static byte[] base642bytes(final String str, final int off, final int len */ public static byte[] zip(byte[] bytes) throws IOException { UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(); - OutputStream os = new DeflaterOutputStream(bos); - try { + try (DeflaterOutputStream os = new DeflaterOutputStream(bos)) { os.write(bytes); } finally { - os.close(); bos.close(); } return bos.toByteArray(); @@ -804,16 +801,11 @@ public static byte[] zip(byte[] bytes) throws IOException { * @throws IOException */ public static byte[] unzip(byte[] bytes) throws IOException { - UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes); - UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(); - InputStream is = new InflaterInputStream(bis); - try { + try (UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes); + UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(); + InflaterInputStream is = new InflaterInputStream(bis)) { IOUtils.write(is, bos); return bos.toByteArray(); - } finally { - is.close(); - bis.close(); - bos.close(); } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java index c2ee5587b3bf..364aa1a8d494 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java @@ -191,13 +191,13 @@ public static String[] readLines(InputStream is) throws IOException { public static String read(InputStream is, String encoding) throws IOException { StringBuilder stringBuilder = new StringBuilder(); - InputStreamReader inputStreamReader = new InputStreamReader(is, encoding); - char[] buf = new char[1024]; - int len; - while ((len = inputStreamReader.read(buf)) != -1) { - stringBuilder.append(buf, 0, len); + try (InputStreamReader inputStreamReader = new InputStreamReader(is, encoding)) { + char[] buf = new char[1024]; + int len; + while ((len = inputStreamReader.read(buf)) != -1) { + stringBuilder.append(buf, 0, len); + } } - inputStreamReader.close(); return stringBuilder.toString(); } 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 c76ea1459aa3..44189f65a9aa 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 @@ -57,17 +57,14 @@ public byte[] compress(byte[] payloadByteArr) throws RpcException { return new byte[0]; } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - BZip2CompressorOutputStream cos; - try { - cos = new BZip2CompressorOutputStream(out); - cos.write(payloadByteArr); - cos.close(); + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + try (BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(out)) { + cos.write(payloadByteArr); + } + return out.toByteArray(); } catch (Exception e) { throw new IllegalStateException(e); } - - return out.toByteArray(); } @Override @@ -85,11 +82,10 @@ public byte[] decompress(byte[] payloadByteArr) { return new byte[0]; } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr); - try { + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr); + BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in)) { int totalBytesRead = 0; - BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in); byte[] buffer = new byte[2048]; int n; while ((n = unZip.read(buffer)) >= 0) { @@ -100,9 +96,9 @@ public byte[] decompress(byte[] payloadByteArr) { } out.write(buffer, 0, n); } + return out.toByteArray(); } catch (Exception e) { throw new IllegalStateException(e); } - return out.toByteArray(); } }