001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org> 002// 003// SPDX-License-Identifier: Apache-2.0 004 005package org.pgpainless.decryption_verification.cleartext_signatures; 006 007import java.io.ByteArrayInputStream; 008import java.io.ByteArrayOutputStream; 009 010/** 011 * Implementation of the {@link MultiPassStrategy}. 012 * This class keeps the read data in memory by caching the data inside a {@link ByteArrayOutputStream}. 013 * 014 * Note, that this class is suitable and efficient for processing small amounts of data. 015 * For larger data like encrypted files, use of the {@link WriteToFileMultiPassStrategy} is recommended to 016 * prevent {@link OutOfMemoryError OutOfMemoryErrors} and other issues. 017 */ 018public class InMemoryMultiPassStrategy implements MultiPassStrategy { 019 020 private final ByteArrayOutputStream cache = new ByteArrayOutputStream(); 021 022 @Override 023 public ByteArrayOutputStream getMessageOutputStream() { 024 return cache; 025 } 026 027 @Override 028 public ByteArrayInputStream getMessageInputStream() { 029 return new ByteArrayInputStream(getBytes()); 030 } 031 032 public byte[] getBytes() { 033 return getMessageOutputStream().toByteArray(); 034 } 035}