From 79b04d15357e45cf7025c9f9c90a29d400e29c48 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 12 Jan 2020 13:11:10 +0100 Subject: [PATCH] Refactor KeyRingReader --- .../pgpainless/key/parsing/KeyRingReader.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java b/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java index f9225eb5..919447b4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java @@ -134,28 +134,41 @@ public class KeyRingReader { } public static PGPKeyRing readKeyRing(@Nullable InputStream publicIn, @Nullable InputStream secretIn) throws IOException, PGPException { + validateStreamsNotBothNull(publicIn, secretIn); + PGPPublicKeyRing publicKeys = maybeReadPublicKeys(publicIn); + PGPSecretKeyRing secretKeys = maybeReadSecretKeys(secretIn); + + return asPGPKeyRing(publicKeys, secretKeys); + } + + private static void validateStreamsNotBothNull(InputStream publicIn, InputStream secretIn) { if (publicIn == null && secretIn == null) { throw new NullPointerException("publicIn and secretIn cannot be BOTH null."); } + } - PGPPublicKeyRing publicKeys = null; + private static PGPPublicKeyRing maybeReadPublicKeys(InputStream publicIn) throws IOException { if (publicIn != null) { - publicKeys = readPublicKeyRing(publicIn); - } - PGPSecretKeyRing secretKeys = null; - if (secretIn != null) { - secretKeys = readSecretKeyRing(secretIn); + return readPublicKeyRing(publicIn); } + return null; + } + private static PGPSecretKeyRing maybeReadSecretKeys(InputStream secretIn) throws IOException, PGPException { + if (secretIn != null) { + return readSecretKeyRing(secretIn); + } + return null; + } + + private static PGPKeyRing asPGPKeyRing(PGPPublicKeyRing publicKeys, PGPSecretKeyRing secretKeys) { if (secretKeys == null) { return new PGPKeyRing(publicKeys); } - if (publicKeys == null) { return new PGPKeyRing(secretKeys); } - return new PGPKeyRing(publicKeys, secretKeys); } }