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); } }