You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/08/04 05:52:04 UTC

[commons-rng] 19/23: Use try with resources

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git

commit 8f4b8be6304fcdffc14799e5eb44e1ad0dd5f8a1
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Aug 3 10:14:16 2021 +0100

    Use try with resources
---
 .../java/org/apache/commons/rng/core/source32/JDKRandom.java   | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/JDKRandom.java b/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/JDKRandom.java
index 9a2fd31..282e549 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/JDKRandom.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/JDKRandom.java
@@ -102,9 +102,8 @@ public class JDKRandom extends IntProvider {
     /** {@inheritDoc} */
     @Override
     protected byte[] getStateInternal() {
-        try {
-            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            final ObjectOutputStream oos = new ObjectOutputStream(bos);
+        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
+             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
 
             // Serialize the "delegate".
             oos.writeObject(delegate);
@@ -133,9 +132,8 @@ public class JDKRandom extends IntProvider {
         final byte[][] c = splitStateInternal(s2[1], stateSize);
 
         // Use look-ahead deserialization to validate the state byte[] contains java.util.Random.
-        try {
-            final ByteArrayInputStream bis = new ByteArrayInputStream(c[0]);
-            final ObjectInputStream ois = new ValidatingObjectInputStream(bis);
+        try (ByteArrayInputStream bis = new ByteArrayInputStream(c[0]);
+             ObjectInputStream ois = new ValidatingObjectInputStream(bis)) {
 
             delegate = (Random) ois.readObject();
         } catch (ClassNotFoundException | IOException e) {