mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 06:12:06 +01:00
Refactor KeyRingReader
This commit is contained in:
parent
6e5dce8261
commit
79b04d1535
1 changed files with 21 additions and 8 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue