001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org> 002// 003// SPDX-License-Identifier: Apache-2.0 004 005package org.pgpainless.decryption_verification; 006 007import java.io.IOException; 008import java.io.InputStream; 009import javax.annotation.Nonnull; 010 011public abstract class CloseForResultInputStream extends InputStream { 012 013 protected final OpenPgpMetadata.Builder resultBuilder; 014 private boolean isClosed = false; 015 016 public CloseForResultInputStream(@Nonnull OpenPgpMetadata.Builder resultBuilder) { 017 this.resultBuilder = resultBuilder; 018 } 019 020 @Override 021 public void close() throws IOException { 022 this.isClosed = true; 023 } 024 025 /** 026 * Return the result of the decryption. 027 * The result contains metadata about the decryption, such as signatures, used keys and algorithms, as well as information 028 * about the decrypted file/stream. 029 * 030 * Can only be obtained once the stream got successfully closed ({@link #close()}). 031 * @return metadata 032 */ 033 public OpenPgpMetadata getResult() { 034 if (!isClosed) { 035 throw new IllegalStateException("Stream MUST be closed before the result can be accessed."); 036 } 037 return resultBuilder.build(); 038 } 039}