@@ -42,20 +42,10 @@ public static void writeByteArrayToFile(File file, byte[] data) throws IOExcepti
42
42
* @since IO 2.1
43
43
*/
44
44
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 )) {
48
46
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
- }
58
47
}
48
+ // ignore
59
49
}
60
50
61
51
/**
@@ -111,10 +101,8 @@ private static boolean isAuthCommand(String command) {
111
101
* @param file the file to save the history
112
102
*/
113
103
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 ) {
118
106
String commandStr = Helper .fromCodePoints (command );
119
107
if (isAuthCommand (commandStr )) {
120
108
command = AUTH_CODEPOINTS ;
@@ -127,20 +115,13 @@ public static void saveCommandHistory(List<int[]> history, File file) {
127
115
}
128
116
} catch (IOException e ) {
129
117
// ignore
130
- } finally {
131
- try {
132
- if (out != null ) {
133
- out .close ();
134
- }
135
- } catch (IOException ioe ) {
136
- // ignore
137
- }
138
118
}
119
+ // ignore
139
120
}
140
121
141
122
public static List <int []> loadCommandHistory (File file ) {
142
123
BufferedReader br = null ;
143
- List <int []> history = new ArrayList <int [] >();
124
+ List <int []> history = new ArrayList <>();
144
125
try {
145
126
br = new BufferedReader (new InputStreamReader (new FileInputStream (file )));
146
127
String line ;
@@ -167,10 +148,8 @@ public static List<int[]> loadCommandHistory(File file) {
167
148
* @param file the file to save the history
168
149
*/
169
150
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 ) {
174
153
if (!StringUtils .isBlank (command )) {
175
154
if (isAuthCommand (command )) {
176
155
command = ArthasConstants .AUTH ;
@@ -181,20 +160,13 @@ public static void saveCommandHistoryString(List<String> history, File file) {
181
160
}
182
161
} catch (IOException e ) {
183
162
// ignore
184
- } finally {
185
- try {
186
- if (out != null ) {
187
- out .close ();
188
- }
189
- } catch (IOException ioe ) {
190
- // ignore
191
- }
192
163
}
164
+ // ignore
193
165
}
194
166
195
167
public static List <String > loadCommandHistoryString (File file ) {
196
168
BufferedReader br = null ;
197
- List <String > history = new ArrayList <String >();
169
+ List <String > history = new ArrayList <>();
198
170
try {
199
171
br = new BufferedReader (new InputStreamReader (new FileInputStream (file ), "utf-8" ));
200
172
String line ;
@@ -218,8 +190,7 @@ public static List<String> loadCommandHistoryString(File file) {
218
190
}
219
191
220
192
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 )) {
223
194
Reader reader = new BufferedReader (new InputStreamReader (stream , encoding ));
224
195
StringBuilder builder = new StringBuilder ();
225
196
char [] buffer = new char [8192 ];
@@ -228,8 +199,6 @@ public static String readFileToString(File file, Charset encoding) throws IOExce
228
199
builder .append (buffer , 0 , read );
229
200
}
230
201
return builder .toString ();
231
- } finally {
232
- stream .close ();
233
202
}
234
203
}
235
204
0 commit comments