1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-16 16:44:50 +02:00
pgpainless/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java

52 lines
1.7 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.decryption_verification;
import java.io.BufferedInputStream;
2018-06-06 18:46:41 +02:00
import java.io.IOException;
import java.io.InputStream;
2020-01-10 18:46:31 +01:00
import javax.annotation.Nonnull;
2018-06-06 18:46:41 +02:00
import org.bouncycastle.openpgp.PGPException;
import org.pgpainless.decryption_verification.cleartext_signatures.VerifyCleartextSignaturesImpl;
import org.pgpainless.exception.WrongConsumingMethodException;
2018-06-06 18:46:41 +02:00
public class DecryptionBuilder implements DecryptionBuilderInterface {
public static int BUFFER_SIZE = 4096;
2020-08-24 14:55:06 +02:00
2018-06-06 18:46:41 +02:00
@Override
public DecryptWith onInputStream(@Nonnull InputStream inputStream) {
return new DecryptWithImpl(inputStream);
2018-06-06 18:46:41 +02:00
}
2021-12-28 13:32:50 +01:00
static class DecryptWithImpl implements DecryptWith {
2018-06-06 18:46:41 +02:00
2021-12-28 12:30:52 +01:00
private final BufferedInputStream inputStream;
DecryptWithImpl(InputStream inputStream) {
this.inputStream = new BufferedInputStream(inputStream, BUFFER_SIZE);
this.inputStream.mark(BUFFER_SIZE);
}
2021-06-15 17:08:40 +02:00
@Override
public DecryptionStream withOptions(ConsumerOptions consumerOptions) throws PGPException, IOException {
if (consumerOptions == null) {
throw new IllegalArgumentException("Consumer options cannot be null.");
}
try {
return DecryptionStreamFactory.create(inputStream, consumerOptions);
} catch (WrongConsumingMethodException e) {
inputStream.reset();
return new VerifyCleartextSignaturesImpl()
.onInputStream(inputStream)
.withOptions(consumerOptions)
.getVerificationStream();
}
2021-06-15 17:08:40 +02:00
}
2018-06-06 18:46:41 +02:00
}
}