LANG-1279: Update Java requirement from Java 6 to 7

use try with resources in SerializationUtils
This commit is contained in:
pascalschumacher 2016-10-23 22:30:00 +02:00
parent dc53e49b4a
commit f9cab271b3
1 changed files with 3 additions and 29 deletions

View File

@ -136,22 +136,10 @@ public class SerializationUtils {
if (outputStream == null) { if (outputStream == null) {
throw new IllegalArgumentException("The OutputStream must not be null"); throw new IllegalArgumentException("The OutputStream must not be null");
} }
ObjectOutputStream out = null; try (ObjectOutputStream out = new ObjectOutputStream(outputStream)){
try {
// stream closed in the finally
out = new ObjectOutputStream(outputStream);
out.writeObject(obj); out.writeObject(obj);
} catch (final IOException ex) { } catch (final IOException ex) {
throw new SerializationException(ex); throw new SerializationException(ex);
} finally {
try {
if (out != null) {
out.close();
}
} catch (final IOException ex) { // NOPMD
// ignore close exception
}
} }
} }
@ -205,26 +193,12 @@ public class SerializationUtils {
if (inputStream == null) { if (inputStream == null) {
throw new IllegalArgumentException("The InputStream must not be null"); throw new IllegalArgumentException("The InputStream must not be null");
} }
ObjectInputStream in = null; try (ObjectInputStream in = new ObjectInputStream(inputStream)) {
try {
// stream closed in the finally
in = new ObjectInputStream(inputStream);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final T obj = (T) in.readObject(); final T obj = (T) in.readObject();
return obj; return obj;
} catch (final ClassNotFoundException | IOException ex) {
} catch (final ClassNotFoundException ex) {
throw new SerializationException(ex); throw new SerializationException(ex);
} catch (final IOException ex) {
throw new SerializationException(ex);
} finally {
try {
if (in != null) {
in.close();
}
} catch (final IOException ex) { // NOPMD
// ignore close exception
}
} }
} }