Skip to content

Commit

Permalink
[ISSUE #8993]Close input stream and output stream by try with resourc…
Browse files Browse the repository at this point in the history
…e[nacos-common] (#8997)

* [ISSUE #8993]Close input stream and output stream by try with resource[nacos-common]

* [ISSUE #8993]Close input stream and output stream by try with resource[nacos-common]

* [ISSUE #8993]Close input stream and output stream by try with resource[nacos-common]
  • Loading branch information
hitcp authored Aug 22, 2022
1 parent 3839cdc commit 3f75b3a
Showing 1 changed file with 13 additions and 41 deletions.
54 changes: 13 additions & 41 deletions common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,12 @@ public class IoUtils {
* @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;
}

/**
Expand All @@ -80,16 +72,10 @@ 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);
}
}

Expand All @@ -102,14 +88,11 @@ public static byte[] tryDecompress(byte[] raw) throws Exception {
*/
public static byte[] tryCompress(String str, String encoding) {
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();
}
Expand All @@ -129,13 +112,9 @@ 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);
}
}

Expand All @@ -149,9 +128,8 @@ public static void writeStringToFile(File file, String data, String encoding) th
public static List<String> readLines(Reader input) throws IOException {
BufferedReader reader = toBufferedReader(input);
List<String> 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());
Expand Down Expand Up @@ -314,15 +292,9 @@ 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);
}
}

Expand Down

0 comments on commit 3f75b3a

Please sign in to comment.