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

try块采用try-with-resource结构 #2758

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public int compare(ThreadVO o1, ThreadVO o2) {

// Sort by CPU time : should be a rendering hint...
Collections.sort(threads, new Comparator<ThreadVO>() {
@Override
public int compare(ThreadVO o1, ThreadVO o2) {
long l1 = deltas.get(o1);
long l2 = deltas.get(o2);
Expand Down
53 changes: 11 additions & 42 deletions core/src/main/java/com/taobao/arthas/core/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,10 @@ public static void writeByteArrayToFile(File file, byte[] data) throws IOExcepti
* @since IO 2.1
*/
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
try (OutputStream out = openOutputStream(file, append)) {
out.write(data);
out.close(); // don't swallow close Exception if copy completes normally
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ioe) {
// ignore
}
}
// ignore
}

/**
Expand Down Expand Up @@ -111,10 +101,8 @@ private static boolean isAuthCommand(String command) {
* @param file the file to save the history
*/
public static void saveCommandHistory(List<int[]> history, File file) {
OutputStream out = null;
try {
out = new BufferedOutputStream(openOutputStream(file, false));
for (int[] command: history) {
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {
for (int[] command : history) {
String commandStr = Helper.fromCodePoints(command);
if (isAuthCommand(commandStr)) {
command = AUTH_CODEPOINTS;
Expand All @@ -127,20 +115,13 @@ public static void saveCommandHistory(List<int[]> history, File file) {
}
} catch (IOException e) {
// ignore
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ioe) {
// ignore
}
}
// ignore
}

public static List<int[]> loadCommandHistory(File file) {
BufferedReader br = null;
List<int[]> history = new ArrayList<int[]>();
List<int[]> history = new ArrayList<>();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
Expand All @@ -167,10 +148,8 @@ public static List<int[]> loadCommandHistory(File file) {
* @param file the file to save the history
*/
public static void saveCommandHistoryString(List<String> history, File file) {
OutputStream out = null;
try {
out = new BufferedOutputStream(openOutputStream(file, false));
for (String command: history) {
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {
for (String command : history) {
if (!StringUtils.isBlank(command)) {
if (isAuthCommand(command)) {
command = ArthasConstants.AUTH;
Expand All @@ -181,20 +160,13 @@ public static void saveCommandHistoryString(List<String> history, File file) {
}
} catch (IOException e) {
// ignore
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ioe) {
// ignore
}
}
// ignore
}

public static List<String> loadCommandHistoryString(File file) {
BufferedReader br = null;
List<String> history = new ArrayList<String>();
List<String> history = new ArrayList<>();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String line;
Expand All @@ -218,8 +190,7 @@ public static List<String> loadCommandHistoryString(File file) {
}

public static String readFileToString(File file, Charset encoding) throws IOException {
FileInputStream stream = new FileInputStream(file);
try {
try (FileInputStream stream = new FileInputStream(file)) {
Reader reader = new BufferedReader(new InputStreamReader(stream, encoding));
StringBuilder builder = new StringBuilder();
char[] buffer = new char[8192];
Expand All @@ -228,8 +199,6 @@ public static String readFileToString(File file, Charset encoding) throws IOExce
builder.append(buffer, 0, read);
}
return builder.toString();
} finally {
stream.close();
}
}

Expand Down