Skip to content

Commit eb9596f

Browse files
authored
Refactored code to adopt JDK 8 style and features. (alibaba#2759)
1 parent 343ac12 commit eb9596f

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

common/src/main/java/com/taobao/arthas/common/JavaVersionUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ public static float javaVersion() {
2828
}
2929

3030
public static boolean isJava6() {
31-
return JAVA_VERSION_STR.equals("1.6");
31+
return "1.6".equals(JAVA_VERSION_STR);
3232
}
3333

3434
public static boolean isJava7() {
35-
return JAVA_VERSION_STR.equals("1.7");
35+
return "1.7".equals(JAVA_VERSION_STR);
3636
}
3737

3838
public static boolean isJava8() {
39-
return JAVA_VERSION_STR.equals("1.8");
39+
return "1.8".equals(JAVA_VERSION_STR);
4040
}
4141

4242
public static boolean isJava9() {
43-
return JAVA_VERSION_STR.equals("9");
43+
return "9".equals(JAVA_VERSION_STR);
4444
}
4545

4646
public static boolean isLessThanJava9() {

core/src/main/java/com/taobao/arthas/core/util/FileUtils.java

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,10 @@ public static void writeByteArrayToFile(File file, byte[] data) throws IOExcepti
4242
* @since IO 2.1
4343
*/
4444
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
45-
OutputStream out = null;
46-
try {
47-
out = openOutputStream(file, append);
45+
try (OutputStream out = openOutputStream(file, append)) {
4846
out.write(data);
49-
out.close(); // don't swallow close Exception if copy completes normally
50-
} finally {
51-
try {
52-
if (out != null) {
53-
out.close();
54-
}
55-
} catch (IOException ioe) {
56-
// ignore
57-
}
5847
}
48+
// ignore
5949
}
6050

6151
/**
@@ -111,10 +101,8 @@ private static boolean isAuthCommand(String command) {
111101
* @param file the file to save the history
112102
*/
113103
public static void saveCommandHistory(List<int[]> history, File file) {
114-
OutputStream out = null;
115-
try {
116-
out = new BufferedOutputStream(openOutputStream(file, false));
117-
for (int[] command: history) {
104+
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {
105+
for (int[] command : history) {
118106
String commandStr = Helper.fromCodePoints(command);
119107
if (isAuthCommand(commandStr)) {
120108
command = AUTH_CODEPOINTS;
@@ -127,20 +115,13 @@ public static void saveCommandHistory(List<int[]> history, File file) {
127115
}
128116
} catch (IOException e) {
129117
// ignore
130-
} finally {
131-
try {
132-
if (out != null) {
133-
out.close();
134-
}
135-
} catch (IOException ioe) {
136-
// ignore
137-
}
138118
}
119+
// ignore
139120
}
140121

141122
public static List<int[]> loadCommandHistory(File file) {
142123
BufferedReader br = null;
143-
List<int[]> history = new ArrayList<int[]>();
124+
List<int[]> history = new ArrayList<>();
144125
try {
145126
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
146127
String line;
@@ -167,10 +148,8 @@ public static List<int[]> loadCommandHistory(File file) {
167148
* @param file the file to save the history
168149
*/
169150
public static void saveCommandHistoryString(List<String> history, File file) {
170-
OutputStream out = null;
171-
try {
172-
out = new BufferedOutputStream(openOutputStream(file, false));
173-
for (String command: history) {
151+
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {
152+
for (String command : history) {
174153
if (!StringUtils.isBlank(command)) {
175154
if (isAuthCommand(command)) {
176155
command = ArthasConstants.AUTH;
@@ -181,20 +160,13 @@ public static void saveCommandHistoryString(List<String> history, File file) {
181160
}
182161
} catch (IOException e) {
183162
// ignore
184-
} finally {
185-
try {
186-
if (out != null) {
187-
out.close();
188-
}
189-
} catch (IOException ioe) {
190-
// ignore
191-
}
192163
}
164+
// ignore
193165
}
194166

195167
public static List<String> loadCommandHistoryString(File file) {
196168
BufferedReader br = null;
197-
List<String> history = new ArrayList<String>();
169+
List<String> history = new ArrayList<>();
198170
try {
199171
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
200172
String line;
@@ -218,8 +190,7 @@ public static List<String> loadCommandHistoryString(File file) {
218190
}
219191

220192
public static String readFileToString(File file, Charset encoding) throws IOException {
221-
FileInputStream stream = new FileInputStream(file);
222-
try {
193+
try (FileInputStream stream = new FileInputStream(file)) {
223194
Reader reader = new BufferedReader(new InputStreamReader(stream, encoding));
224195
StringBuilder builder = new StringBuilder();
225196
char[] buffer = new char[8192];
@@ -228,8 +199,6 @@ public static String readFileToString(File file, Charset encoding) throws IOExce
228199
builder.append(buffer, 0, read);
229200
}
230201
return builder.toString();
231-
} finally {
232-
stream.close();
233202
}
234203
}
235204

0 commit comments

Comments
 (0)