From bf784fe19db5d5fc4845366fd8af2dedac495109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Wed, 31 Jan 2024 14:39:59 +0800 Subject: [PATCH] refactor: use addSuppressed --- src/main/java/com/fingerprint/Sealed.java | 14 +------------- src/test/java/com/fingerprint/SealedTest.java | 4 ++-- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/fingerprint/Sealed.java b/src/main/java/com/fingerprint/Sealed.java index 33b349a..f8b260b 100644 --- a/src/main/java/com/fingerprint/Sealed.java +++ b/src/main/java/com/fingerprint/Sealed.java @@ -9,10 +9,8 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; -import java.util.List; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; @@ -40,19 +38,9 @@ public String toString() { } public static class UnsealAggregateException extends Exception { - private final List unsealExceptions = new ArrayList<>(); - public UnsealAggregateException() { super("Failed to unseal with all decryption keys"); } - - public void addUnsealException(UnsealException exception) { - unsealExceptions.add(exception); - } - - public List getUnsealExceptions() { - return unsealExceptions; - } } public static class InvalidSealedDataException extends IllegalArgumentException { @@ -102,7 +90,7 @@ public static byte[] unseal(byte[] sealed, DecryptionKey[] keys) throws IllegalA try { return decryptAes256Gcm(Arrays.copyOfRange(sealed, SEAL_HEADER.length, sealed.length), key.key); } catch (Exception exception) { - aggregateException.addUnsealException( + aggregateException.addSuppressed( new UnsealException( "Failed to decrypt", exception, diff --git a/src/test/java/com/fingerprint/SealedTest.java b/src/test/java/com/fingerprint/SealedTest.java index 6354c8a..af1fa25 100644 --- a/src/test/java/com/fingerprint/SealedTest.java +++ b/src/test/java/com/fingerprint/SealedTest.java @@ -114,7 +114,7 @@ public void unsealEventResponseWithNotCompressedSealedResultTest() throws Except } )); - Sealed.UnsealException unsealException = exception.getUnsealExceptions().get(2); + Sealed.UnsealException unsealException = (Sealed.UnsealException) exception.getSuppressed()[2]; assertEquals(unsealException.getCause().getMessage(), "invalid distance too far back"); } @@ -220,7 +220,7 @@ public void unsealEventResponseWithInvalidNonceTest() throws Exception { } )); - Sealed.UnsealException unsealException = exception.getUnsealExceptions().get(2); + Sealed.UnsealException unsealException = (Sealed.UnsealException) exception.getSuppressed()[2]; assertEquals(unsealException.getCause().getMessage(), "12 > 3"); }