001// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org> 002// 003// SPDX-License-Identifier: Apache-2.0 004 005package org.pgpainless.decryption_verification; 006 007import java.io.BufferedInputStream; 008import java.io.IOException; 009import java.io.InputStream; 010import javax.annotation.Nonnull; 011 012import org.bouncycastle.openpgp.PGPException; 013import org.pgpainless.decryption_verification.cleartext_signatures.VerifyCleartextSignaturesImpl; 014import org.pgpainless.exception.WrongConsumingMethodException; 015 016public class DecryptionBuilder implements DecryptionBuilderInterface { 017 018 public static int BUFFER_SIZE = 4096; 019 020 @Override 021 public DecryptWith onInputStream(@Nonnull InputStream inputStream) { 022 return new DecryptWithImpl(inputStream); 023 } 024 025 static class DecryptWithImpl implements DecryptWith { 026 027 private final BufferedInputStream inputStream; 028 029 DecryptWithImpl(InputStream inputStream) { 030 this.inputStream = new BufferedInputStream(inputStream, BUFFER_SIZE); 031 this.inputStream.mark(BUFFER_SIZE); 032 } 033 034 @Override 035 public DecryptionStream withOptions(ConsumerOptions consumerOptions) throws PGPException, IOException { 036 if (consumerOptions == null) { 037 throw new IllegalArgumentException("Consumer options cannot be null."); 038 } 039 040 try { 041 return DecryptionStreamFactory.create(inputStream, consumerOptions); 042 } catch (WrongConsumingMethodException e) { 043 inputStream.reset(); 044 return new VerifyCleartextSignaturesImpl() 045 .onInputStream(inputStream) 046 .withOptions(consumerOptions) 047 .getVerificationStream(); 048 } 049 } 050 } 051}