From 9f95e7925b32b58caaf34372cbae6f34ead7bc0c Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 18 Nov 2020 12:20:59 +0100 Subject: [PATCH] De-duplicate KeyPrinter class --- .../java/org/pgpainless/util/KeyPrinter.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/KeyPrinter.java b/pgpainless-core/src/main/java/org/pgpainless/util/KeyPrinter.java index 14f6510a..c6458d22 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/KeyPrinter.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/KeyPrinter.java @@ -18,6 +18,7 @@ package org.pgpainless.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.openpgp.PGPPublicKeyRing; @@ -27,20 +28,22 @@ import org.bouncycastle.util.io.Streams; public class KeyPrinter { public static String toAsciiArmoredString(PGPSecretKeyRing secretKeys) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArmoredOutputStream armor = new ArmoredOutputStream(out); - - Streams.pipeAll(new ByteArrayInputStream(secretKeys.getEncoded()), armor); - armor.close(); - - return out.toString(); + return toAsciiArmoredString(secretKeys.getEncoded()); } public static String toAsciiArmoredString(PGPPublicKeyRing publicKeys) throws IOException { + return toAsciiArmoredString(publicKeys.getEncoded()); + } + + public static String toAsciiArmoredString(byte[] bytes) throws IOException { + return toAsciiArmoredString(new ByteArrayInputStream(bytes)); + } + + public static String toAsciiArmoredString(InputStream inputStream) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ArmoredOutputStream armor = new ArmoredOutputStream(out); - Streams.pipeAll(new ByteArrayInputStream(publicKeys.getEncoded()), armor); + Streams.pipeAll(inputStream, armor); armor.close(); return out.toString();