1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-29 15:52:08 +01:00

Refactor KeyRingReader

This commit is contained in:
Paul Schaub 2020-01-12 13:11:10 +01:00
parent 6e5dce8261
commit 79b04d1535
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

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