From 5b1b1bae37318a917c7a27a88ab5870393ded1d2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 3 May 2019 00:11:47 +0200 Subject: [PATCH] Consistent use of try-with-resources for local resource closing --- .../util/SerializationUtils.java | 8 +++----- .../util/SerializationTestUtils.java | 20 ++++++++++--------- .../AbstractEmbeddedDatabaseConfigurer.java | 7 +++++-- .../http/MediaTypeFactory.java | 6 +++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/SerializationUtils.java b/spring-core/src/main/java/org/springframework/util/SerializationUtils.java index 65d65be0ff6..1f15a163f0e 100644 --- a/spring-core/src/main/java/org/springframework/util/SerializationUtils.java +++ b/spring-core/src/main/java/org/springframework/util/SerializationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,8 +43,7 @@ public abstract class SerializationUtils { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); - try { - ObjectOutputStream oos = new ObjectOutputStream(baos); + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(object); oos.flush(); } @@ -64,8 +63,7 @@ public abstract class SerializationUtils { if (bytes == null) { return null; } - try { - ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); + try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { return ois.readObject(); } catch (IOException ex) { diff --git a/spring-core/src/test/java/org/springframework/util/SerializationTestUtils.java b/spring-core/src/test/java/org/springframework/util/SerializationTestUtils.java index ce4eed15584..38c48dea347 100644 --- a/spring-core/src/test/java/org/springframework/util/SerializationTestUtils.java +++ b/spring-core/src/test/java/org/springframework/util/SerializationTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,8 +34,9 @@ public class SerializationTestUtils { public static void testSerialization(Object o) throws IOException { OutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(o); + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(o); + } } public static boolean isSerializable(Object o) throws IOException { @@ -50,16 +51,17 @@ public class SerializationTestUtils { public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(o); - oos.flush(); + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(o); + oos.flush(); + } baos.flush(); byte[] bytes = baos.toByteArray(); ByteArrayInputStream is = new ByteArrayInputStream(bytes); - ObjectInputStream ois = new ObjectInputStream(is); - Object o2 = ois.readObject(); - return o2; + try (ObjectInputStream ois = new ObjectInputStream(is)) { + return ois.readObject(); + } } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java index cecc0a9186b..15aba492e09 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.jdbc.datasource.embedded; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -42,7 +43,9 @@ abstract class AbstractEmbeddedDatabaseConfigurer implements EmbeddedDatabaseCon try { con = dataSource.getConnection(); if (con != null) { - con.createStatement().execute("SHUTDOWN"); + try (Statement stmt = con.createStatement()) { + stmt.execute("SHUTDOWN"); + } } } catch (SQLException ex) { diff --git a/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java b/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java index 0a05e8ea83e..7ad0969f2b9 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java +++ b/spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,8 +64,8 @@ public final class MediaTypeFactory { * @return a multi-value map, mapping media types to file extensions. */ private static MultiValueMap parseMimeTypes() { - try (InputStream is = MediaTypeFactory.class.getResourceAsStream(MIME_TYPES_FILE_NAME)) { - BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII)); + InputStream is = MediaTypeFactory.class.getResourceAsStream(MIME_TYPES_FILE_NAME); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) { MultiValueMap result = new LinkedMultiValueMap<>(); String line; while ((line = reader.readLine()) != null) {