1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-09-27 18:19:34 +02:00
pgpainless/pgpainless-core/src/main/java/org/pgpainless/ascii_armor/ArmoredOutputStreamFactory.java

89 lines
2.8 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.ascii_armor;
import java.io.OutputStream;
import org.bouncycastle.bcpg.ArmoredOutputStream;
/**
* Factory to create configured {@link ArmoredOutputStream ArmoredOutputStreams}.
* The configuration entails setting custom version and comment headers.
*/
2021-08-15 15:24:19 +02:00
public final class ArmoredOutputStreamFactory {
public static final String PGPAINLESS = "PGPainless";
2021-08-15 15:35:22 +02:00
private static String version = PGPAINLESS;
private static String[] comment = new String[0];
2021-08-15 15:24:19 +02:00
private ArmoredOutputStreamFactory() {
}
/**
* Wrap an {@link OutputStream} inside a preconfigured {@link ArmoredOutputStream}.
*
* @param outputStream inner stream
* @return armored output stream
*/
public static ArmoredOutputStream get(OutputStream outputStream) {
ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream);
2021-08-15 15:35:22 +02:00
armoredOutputStream.setHeader(ArmorUtils.HEADER_VERSION, version);
for (String comment : comment) {
ArmorUtils.addCommentHeader(armoredOutputStream, comment);
}
return armoredOutputStream;
}
/**
* Overwrite the version header of ASCII armors with a custom value.
* Newlines in the version info string result in multiple version header entries.
*
2021-08-15 15:35:22 +02:00
* @param versionString version string
*/
2021-08-15 15:35:22 +02:00
public static void setVersionInfo(String versionString) {
if (versionString == null || versionString.trim().isEmpty()) {
throw new IllegalArgumentException("Version Info MUST NOT be null NOR empty.");
}
2021-08-15 15:35:22 +02:00
version = versionString;
}
/**
* Reset the version header to its default value of {@link #PGPAINLESS}.
*/
public static void resetVersionInfo() {
2021-08-15 15:35:22 +02:00
version = PGPAINLESS;
}
/**
* Set a comment header value in the ASCII armor header.
* If the comment contains newlines, it will be split into multiple header entries.
*
2022-02-24 01:08:23 +01:00
* @see org.pgpainless.encryption_signing.ProducerOptions#setComment(String) for how to set comments for
* individual messages.
*
2021-08-15 15:35:22 +02:00
* @param commentString comment
*/
2021-08-15 15:35:22 +02:00
public static void setComment(String commentString) {
if (commentString == null) {
throw new IllegalArgumentException("Comment cannot be null.");
}
2021-08-15 15:35:22 +02:00
String trimmed = commentString.trim();
if (trimmed.isEmpty()) {
throw new IllegalArgumentException("Comment cannot be empty.");
}
2021-08-15 15:35:22 +02:00
String[] lines = commentString.split("\n");
comment = lines;
}
/**
* Reset to the default of no comment headers.
*/
public static void resetComment() {
2021-08-15 15:35:22 +02:00
comment = new String[0];
}
}