Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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