Skip to content

Commit 5b1b1ba

Browse files
committed
Consistent use of try-with-resources for local resource closing
1 parent 31c3b8a commit 5b1b1ba

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

spring-core/src/main/java/org/springframework/util/SerializationUtils.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,8 +43,7 @@ public static byte[] serialize(@Nullable Object object) {
4343
return null;
4444
}
4545
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
46-
try {
47-
ObjectOutputStream oos = new ObjectOutputStream(baos);
46+
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
4847
oos.writeObject(object);
4948
oos.flush();
5049
}
@@ -64,8 +63,7 @@ public static Object deserialize(@Nullable byte[] bytes) {
6463
if (bytes == null) {
6564
return null;
6665
}
67-
try {
68-
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
66+
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
6967
return ois.readObject();
7068
}
7169
catch (IOException ex) {

spring-core/src/test/java/org/springframework/util/SerializationTestUtils.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,8 +34,9 @@ public class SerializationTestUtils {
3434

3535
public static void testSerialization(Object o) throws IOException {
3636
OutputStream baos = new ByteArrayOutputStream();
37-
ObjectOutputStream oos = new ObjectOutputStream(baos);
38-
oos.writeObject(o);
37+
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
38+
oos.writeObject(o);
39+
}
3940
}
4041

4142
public static boolean isSerializable(Object o) throws IOException {
@@ -50,16 +51,17 @@ public static boolean isSerializable(Object o) throws IOException {
5051

5152
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
5253
ByteArrayOutputStream baos = new ByteArrayOutputStream();
53-
ObjectOutputStream oos = new ObjectOutputStream(baos);
54-
oos.writeObject(o);
55-
oos.flush();
54+
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
55+
oos.writeObject(o);
56+
oos.flush();
57+
}
5658
baos.flush();
5759
byte[] bytes = baos.toByteArray();
5860

5961
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
60-
ObjectInputStream ois = new ObjectInputStream(is);
61-
Object o2 = ois.readObject();
62-
return o2;
62+
try (ObjectInputStream ois = new ObjectInputStream(is)) {
63+
return ois.readObject();
64+
}
6365
}
6466

6567
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.sql.Connection;
2020
import java.sql.SQLException;
21+
import java.sql.Statement;
2122
import javax.sql.DataSource;
2223

2324
import org.apache.commons.logging.Log;
@@ -42,7 +43,9 @@ public void shutdown(DataSource dataSource, String databaseName) {
4243
try {
4344
con = dataSource.getConnection();
4445
if (con != null) {
45-
con.createStatement().execute("SHUTDOWN");
46+
try (Statement stmt = con.createStatement()) {
47+
stmt.execute("SHUTDOWN");
48+
}
4649
}
4750
}
4851
catch (SQLException ex) {

spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,8 +64,8 @@ private MediaTypeFactory() {
6464
* @return a multi-value map, mapping media types to file extensions.
6565
*/
6666
private static MultiValueMap<String, MediaType> parseMimeTypes() {
67-
try (InputStream is = MediaTypeFactory.class.getResourceAsStream(MIME_TYPES_FILE_NAME)) {
68-
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII));
67+
InputStream is = MediaTypeFactory.class.getResourceAsStream(MIME_TYPES_FILE_NAME);
68+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
6969
MultiValueMap<String, MediaType> result = new LinkedMultiValueMap<>();
7070
String line;
7171
while ((line = reader.readLine()) != null) {

0 commit comments

Comments
 (0)