From e08bf1d478401e6ef217844e8fde1becd3a07546 Mon Sep 17 00:00:00 2001 From: Shaoyu Liu <455028117@qq.com> Date: Thu, 18 Aug 2022 20:40:07 +0800 Subject: [PATCH 1/3] [ISSUE #8993]Close input stream and output stream by try with resource[nacos-common] --- .../alibaba/nacos/common/utils/IoUtils.java | 110 +++++++----------- 1 file changed, 40 insertions(+), 70 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 28c47576d07..0b5bc5aee4b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -46,29 +46,21 @@ * @author nacos */ public class IoUtils { - + /** * Try decompress by GZIP from stream. * * @param raw compress stream * @return byte array after decompress */ - public static byte[] tryDecompress(InputStream raw) { - GZIPInputStream gis = null; - ByteArrayOutputStream out = null; - try { - gis = new GZIPInputStream(raw); - out = new ByteArrayOutputStream(); + public static byte[] tryDecompress(InputStream raw) throws IOException { + try (GZIPInputStream gis = new GZIPInputStream(raw); + ByteArrayOutputStream out = new ByteArrayOutputStream()) { copy(gis, out); return out.toByteArray(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - closeQuietly(out, gis); } - return null; } - + /** * Try decompress by GZIP from byte array. * @@ -80,19 +72,13 @@ public static byte[] tryDecompress(byte[] raw) throws Exception { if (!isGzipStream(raw)) { return raw; } - GZIPInputStream gis = null; - ByteArrayOutputStream out = null; - - try { - gis = new GZIPInputStream(new ByteArrayInputStream(raw)); - out = new ByteArrayOutputStream(); - IoUtils.copy(gis, out); + try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(raw)); + ByteArrayOutputStream out = new ByteArrayOutputStream()) { + copy(gis, out); return out.toByteArray(); - } finally { - closeQuietly(out, gis); } } - + /** * Try compress by GZIP for string. * @@ -100,26 +86,21 @@ public static byte[] tryDecompress(byte[] raw) throws Exception { * @param encoding encoding. * @return byte[] */ - public static byte[] tryCompress(String str, String encoding) { + public static byte[] tryCompress(String str, String encoding) throws Exception { if (str == null || str.length() == 0) { - return null; + return new byte[0]; } ByteArrayOutputStream out = new ByteArrayOutputStream(); - GZIPOutputStream gzip; - try { - gzip = new GZIPOutputStream(out); + try (GZIPOutputStream gzip = new GZIPOutputStream(out)) { gzip.write(str.getBytes(encoding)); - gzip.close(); - } catch (Exception e) { - e.printStackTrace(); } return out.toByteArray(); } - + private static BufferedReader toBufferedReader(Reader reader) { return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); } - + /** * Write string to a file. * @@ -129,16 +110,12 @@ private static BufferedReader toBufferedReader(Reader reader) { * @throws IOException io exception */ public static void writeStringToFile(File file, String data, String encoding) throws IOException { - OutputStream os = null; - try { - os = new FileOutputStream(file); + try(OutputStream os = new FileOutputStream(file)) { os.write(data.getBytes(encoding)); os.flush(); - } finally { - closeQuietly(os); } } - + /** * Read lines. * @@ -149,9 +126,8 @@ public static void writeStringToFile(File file, String data, String encoding) th public static List readLines(Reader input) throws IOException { BufferedReader reader = toBufferedReader(input); List list = new ArrayList<>(); - String line = null; - for (; ; ) { - line = reader.readLine(); + while (true) { + String line = reader.readLine(); if (null != line) { if (StringUtils.isNotEmpty(line)) { list.add(line.trim()); @@ -162,7 +138,7 @@ public static List readLines(Reader input) throws IOException { } return list; } - + /** * To string from stream. * @@ -178,7 +154,7 @@ public static String toString(InputStream input, String encoding) throws IOExcep return (null == encoding) ? toString(new InputStreamReader(input, Constants.ENCODE)) : toString(new InputStreamReader(input, encoding)); } - + /** * To string from reader. * @@ -191,7 +167,7 @@ public static String toString(Reader reader) throws IOException { copy(reader, sw); return sw.toString(); } - + /** * Copy data. * @@ -209,7 +185,7 @@ public static long copy(Reader input, Writer output) throws IOException { } return count; } - + /** * Copy data. * @@ -224,13 +200,13 @@ public static long copy(InputStream input, OutputStream output) throws IOExcepti int totalBytes = 0; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); - + totalBytes += bytesRead; } - + return totalBytes; } - + /** * Delete file or dir. * @@ -245,7 +221,7 @@ public static void delete(File fileOrDir) throws IOException { if (fileOrDir == null) { return; } - + if (fileOrDir.isDirectory()) { cleanDirectory(fileOrDir); } else { @@ -257,7 +233,7 @@ public static void delete(File fileOrDir) throws IOException { } } } - + /** * 清理目录下的内容. Clean content under directory. * @@ -269,18 +245,18 @@ public static void cleanDirectory(File directory) throws IOException { String message = directory + " does not exist"; throw new IllegalArgumentException(message); } - + if (!directory.isDirectory()) { String message = directory + " is not a directory"; throw new IllegalArgumentException(message); } - + File[] files = directory.listFiles(); // null if security restricted if (files == null) { throw new IOException("Failed to list contents of " + directory); } - + IOException exception = null; for (File file : files) { try { @@ -289,12 +265,12 @@ public static void cleanDirectory(File directory) throws IOException { exception = ioe; } } - + if (null != exception) { throw exception; } } - + /** * Copy File. * @@ -314,18 +290,12 @@ public static void copyFile(String source, String target) throws IOException { if (!tf.exists() && !tf.createNewFile()) { throw new RuntimeException("failed to create target file."); } - - FileChannel sc = null; - FileChannel tc = null; - try { - tc = new FileOutputStream(tf).getChannel(); - sc = new FileInputStream(sf).getChannel(); + try (FileChannel sc = new FileInputStream(sf).getChannel(); + FileChannel tc = new FileOutputStream(tf).getChannel()) { sc.transferTo(0, sc.size(), tc); - } finally { - closeQuietly(sc, tc); } } - + /** * Judge whether is Gzip stream. * @@ -333,15 +303,15 @@ public static void copyFile(String source, String target) throws IOException { * @return true if is gzip, otherwise false */ public static boolean isGzipStream(byte[] bytes) { - + int minByteArraySize = 2; if (bytes == null || bytes.length < minByteArraySize) { return false; } - + return GZIPInputStream.GZIP_MAGIC == ((bytes[1] << 8 | bytes[0]) & 0xFFFF); } - + /** * Close http connection quietly. * @@ -355,7 +325,7 @@ public static void closeQuietly(HttpURLConnection connection) { } } } - + /** * Close closable object quietly. * @@ -369,7 +339,7 @@ public static void closeQuietly(Closeable closeable) { } catch (IOException ignored) { } } - + public static void closeQuietly(Closeable... closeable) { Arrays.stream(closeable).forEach(IoUtils::closeQuietly); } From 62bfca107f1ac12cdde19884888541c4d104698a Mon Sep 17 00:00:00 2001 From: Shaoyu Liu <455028117@qq.com> Date: Thu, 18 Aug 2022 21:14:48 +0800 Subject: [PATCH 2/3] [ISSUE #8993]Close input stream and output stream by try with resource[nacos-common] --- .../alibaba/nacos/common/utils/IoUtils.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 0b5bc5aee4b..fca08a2c82a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -46,7 +46,7 @@ * @author nacos */ public class IoUtils { - + /** * Try decompress by GZIP from stream. * @@ -55,12 +55,12 @@ public class IoUtils { */ public static byte[] tryDecompress(InputStream raw) throws IOException { try (GZIPInputStream gis = new GZIPInputStream(raw); - ByteArrayOutputStream out = new ByteArrayOutputStream()) { + ByteArrayOutputStream out = new ByteArrayOutputStream()) { copy(gis, out); return out.toByteArray(); } } - + /** * Try decompress by GZIP from byte array. * @@ -73,12 +73,12 @@ public static byte[] tryDecompress(byte[] raw) throws Exception { return raw; } try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(raw)); - ByteArrayOutputStream out = new ByteArrayOutputStream()) { + ByteArrayOutputStream out = new ByteArrayOutputStream()) { copy(gis, out); return out.toByteArray(); } } - + /** * Try compress by GZIP for string. * @@ -96,11 +96,11 @@ public static byte[] tryCompress(String str, String encoding) throws Exception { } return out.toByteArray(); } - + private static BufferedReader toBufferedReader(Reader reader) { return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); } - + /** * Write string to a file. * @@ -110,12 +110,12 @@ private static BufferedReader toBufferedReader(Reader reader) { * @throws IOException io exception */ public static void writeStringToFile(File file, String data, String encoding) throws IOException { - try(OutputStream os = new FileOutputStream(file)) { + try (OutputStream os = new FileOutputStream(file)) { os.write(data.getBytes(encoding)); os.flush(); } } - + /** * Read lines. * @@ -138,7 +138,7 @@ public static List readLines(Reader input) throws IOException { } return list; } - + /** * To string from stream. * @@ -154,7 +154,7 @@ public static String toString(InputStream input, String encoding) throws IOExcep return (null == encoding) ? toString(new InputStreamReader(input, Constants.ENCODE)) : toString(new InputStreamReader(input, encoding)); } - + /** * To string from reader. * @@ -167,7 +167,7 @@ public static String toString(Reader reader) throws IOException { copy(reader, sw); return sw.toString(); } - + /** * Copy data. * @@ -185,7 +185,7 @@ public static long copy(Reader input, Writer output) throws IOException { } return count; } - + /** * Copy data. * @@ -200,13 +200,13 @@ public static long copy(InputStream input, OutputStream output) throws IOExcepti int totalBytes = 0; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); - + totalBytes += bytesRead; } - + return totalBytes; } - + /** * Delete file or dir. * @@ -221,7 +221,7 @@ public static void delete(File fileOrDir) throws IOException { if (fileOrDir == null) { return; } - + if (fileOrDir.isDirectory()) { cleanDirectory(fileOrDir); } else { @@ -233,7 +233,7 @@ public static void delete(File fileOrDir) throws IOException { } } } - + /** * 清理目录下的内容. Clean content under directory. * @@ -245,18 +245,18 @@ public static void cleanDirectory(File directory) throws IOException { String message = directory + " does not exist"; throw new IllegalArgumentException(message); } - + if (!directory.isDirectory()) { String message = directory + " is not a directory"; throw new IllegalArgumentException(message); } - + File[] files = directory.listFiles(); // null if security restricted if (files == null) { throw new IOException("Failed to list contents of " + directory); } - + IOException exception = null; for (File file : files) { try { @@ -265,12 +265,12 @@ public static void cleanDirectory(File directory) throws IOException { exception = ioe; } } - + if (null != exception) { throw exception; } } - + /** * Copy File. * @@ -291,11 +291,11 @@ public static void copyFile(String source, String target) throws IOException { throw new RuntimeException("failed to create target file."); } try (FileChannel sc = new FileInputStream(sf).getChannel(); - FileChannel tc = new FileOutputStream(tf).getChannel()) { + FileChannel tc = new FileOutputStream(tf).getChannel()) { sc.transferTo(0, sc.size(), tc); } } - + /** * Judge whether is Gzip stream. * @@ -303,15 +303,15 @@ public static void copyFile(String source, String target) throws IOException { * @return true if is gzip, otherwise false */ public static boolean isGzipStream(byte[] bytes) { - + int minByteArraySize = 2; if (bytes == null || bytes.length < minByteArraySize) { return false; } - + return GZIPInputStream.GZIP_MAGIC == ((bytes[1] << 8 | bytes[0]) & 0xFFFF); } - + /** * Close http connection quietly. * @@ -325,7 +325,7 @@ public static void closeQuietly(HttpURLConnection connection) { } } } - + /** * Close closable object quietly. * @@ -339,7 +339,7 @@ public static void closeQuietly(Closeable closeable) { } catch (IOException ignored) { } } - + public static void closeQuietly(Closeable... closeable) { Arrays.stream(closeable).forEach(IoUtils::closeQuietly); } From 2a15bb7d80a3879c76d62669fb48452149d1fc43 Mon Sep 17 00:00:00 2001 From: Shaoyu Liu <455028117@qq.com> Date: Thu, 18 Aug 2022 21:23:05 +0800 Subject: [PATCH 3/3] [ISSUE #8993]Close input stream and output stream by try with resource[nacos-common] --- .../src/main/java/com/alibaba/nacos/common/utils/IoUtils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index fca08a2c82a..68ffb029b2f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -86,13 +86,15 @@ public static byte[] tryDecompress(byte[] raw) throws Exception { * @param encoding encoding. * @return byte[] */ - public static byte[] tryCompress(String str, String encoding) throws Exception { + public static byte[] tryCompress(String str, String encoding) { if (str == null || str.length() == 0) { return new byte[0]; } ByteArrayOutputStream out = new ByteArrayOutputStream(); try (GZIPOutputStream gzip = new GZIPOutputStream(out)) { gzip.write(str.getBytes(encoding)); + } catch (Exception e) { + e.printStackTrace(); } return out.toByteArray(); }