package org.bouncycastle.openpgp.api;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.PacketFormat;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPadding;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.util.io.TeeOutputStream;

/* JADX INFO: loaded from: classes2.dex */
public class OpenPGPMessageOutputStream extends OutputStream {
    private final OutputStream armorOut;
    private final OutputStream baseOut;
    private final OutputStream compressOut;
    private final OutputStream encodeOut;
    private final OutputStream encryptOut;
    private final OutputStream literalOut;
    private final OutputStream paddingOut;
    private final OutputStream plaintextOut;
    private final OutputStream signOut;

    /* JADX INFO: loaded from: classes3.dex */
    static class Builder {
        private OpenPGPMessageGenerator.ArmoredOutputStreamFactory armorFactory;
        private OutputStreamFactory compressionStreamFactory;
        private OutputStreamFactory encryptionStreamFactory;
        private OutputStreamFactory literalDataStreamFactory;
        private OutputStreamFactory paddingStreamFactory;
        private OutputStreamFactory signatureStreamFactory;

        Builder() {
        }

        public Builder armor(OpenPGPMessageGenerator.ArmoredOutputStreamFactory armoredOutputStreamFactory) {
            this.armorFactory = armoredOutputStreamFactory;
            return this;
        }

        public OpenPGPMessageOutputStream build(OutputStream outputStream) throws IOException, PGPException {
            return new OpenPGPMessageOutputStream(outputStream, this);
        }

        public Builder compress(OutputStreamFactory outputStreamFactory) {
            this.compressionStreamFactory = outputStreamFactory;
            return this;
        }

        public Builder encrypt(OutputStreamFactory outputStreamFactory) {
            this.encryptionStreamFactory = outputStreamFactory;
            return this;
        }

        public Builder literalData(OutputStreamFactory outputStreamFactory) {
            this.literalDataStreamFactory = outputStreamFactory;
            return this;
        }

        public Builder padding(OutputStreamFactory outputStreamFactory) {
            this.paddingStreamFactory = outputStreamFactory;
            return this;
        }

        public Builder sign(OutputStreamFactory outputStreamFactory) {
            this.signatureStreamFactory = outputStreamFactory;
            return this;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public interface OutputStreamFactory {
        OutputStream get(OutputStream outputStream) throws IOException, PGPException;
    }

    /* JADX INFO: loaded from: classes.dex */
    static class PaddingPacketAppenderOutputStream extends OutputStream {
        private final OutputStream out;
        private final PaddingPacketFactory packetFactory;

        public PaddingPacketAppenderOutputStream(OutputStream outputStream, PaddingPacketFactory paddingPacketFactory) {
            this.out = outputStream;
            this.packetFactory = paddingPacketFactory;
        }

        @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            this.packetFactory.providePaddingPacket().encode(this.out);
            this.out.close();
        }

        @Override // java.io.OutputStream
        public void write(int i) throws IOException {
            this.out.write(i);
        }

        @Override // java.io.OutputStream
        public void write(byte[] bArr) throws IOException {
            this.out.write(bArr);
        }

        @Override // java.io.OutputStream
        public void write(byte[] bArr, int i, int i2) throws IOException {
            this.out.write(bArr, i, i2);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public interface PaddingPacketFactory {
        PGPPadding providePaddingPacket();
    }

    /* JADX INFO: loaded from: classes3.dex */
    static class SignatureGeneratorOutputStream extends OutputStream {
        private final OutputStream out;
        private final List<PGPSignatureGenerator> signatureGenerators;

        public SignatureGeneratorOutputStream(OutputStream outputStream, List<PGPSignatureGenerator> list) {
            this.out = outputStream;
            this.signatureGenerators = list;
        }

        @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            for (int size = this.signatureGenerators.size() - 1; size >= 0; size--) {
                try {
                    this.signatureGenerators.get(size).generate().encode(this.out);
                } catch (PGPException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        @Override // java.io.OutputStream
        public void write(int i) throws IOException {
            Iterator<PGPSignatureGenerator> it = this.signatureGenerators.iterator();
            while (it.hasNext()) {
                it.next().update((byte) i);
            }
        }

        @Override // java.io.OutputStream
        public void write(byte[] bArr) throws IOException {
            Iterator<PGPSignatureGenerator> it = this.signatureGenerators.iterator();
            while (it.hasNext()) {
                it.next().update(bArr);
            }
        }

        @Override // java.io.OutputStream
        public void write(byte[] bArr, int i, int i2) throws IOException {
            Iterator<PGPSignatureGenerator> it = this.signatureGenerators.iterator();
            while (it.hasNext()) {
                it.next().update(bArr, i, i2);
            }
        }
    }

    OpenPGPMessageOutputStream(OutputStream outputStream, Builder builder) throws IOException, PGPException {
        this.baseOut = outputStream;
        if (builder.armorFactory != null) {
            this.armorOut = builder.armorFactory.get(outputStream);
            outputStream = this.armorOut;
        } else {
            this.armorOut = null;
        }
        this.encodeOut = new BCPGOutputStream(outputStream, PacketFormat.CURRENT);
        OutputStream outputStream2 = this.encodeOut;
        if (builder.encryptionStreamFactory != null) {
            this.encryptOut = builder.encryptionStreamFactory.get(outputStream2);
            outputStream2 = this.encryptOut;
        } else {
            this.encryptOut = null;
        }
        if (builder.paddingStreamFactory != null) {
            this.paddingOut = builder.paddingStreamFactory.get(outputStream2);
            outputStream2 = this.paddingOut;
        } else {
            this.paddingOut = null;
        }
        if (builder.signatureStreamFactory != null) {
            this.signOut = builder.signatureStreamFactory.get(outputStream2);
        } else {
            this.signOut = null;
        }
        if (builder.compressionStreamFactory != null) {
            this.compressOut = builder.compressionStreamFactory.get(outputStream2);
            outputStream2 = this.compressOut;
        } else {
            this.compressOut = null;
        }
        if (builder.literalDataStreamFactory == null) {
            throw new PGPException("Missing instructions for LiteralData encoding.");
        }
        this.literalOut = builder.literalDataStreamFactory.get(outputStream2);
        this.plaintextOut = this.signOut != null ? new TeeOutputStream(this.literalOut, this.signOut) : this.literalOut;
    }

    static Builder builder() {
        return new Builder();
    }

    @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.literalOut.close();
        if (this.compressOut != null) {
            this.compressOut.close();
        }
        if (this.signOut != null) {
            this.signOut.close();
        }
        if (this.paddingOut != null) {
            this.paddingOut.close();
        }
        if (this.encryptOut != null) {
            this.encryptOut.close();
        }
        this.encodeOut.close();
        if (this.armorOut != null) {
            this.armorOut.close();
        }
        this.baseOut.close();
    }

    @Override // java.io.OutputStream, java.io.Flushable
    public void flush() throws IOException {
        this.literalOut.flush();
        if (this.compressOut != null) {
            this.compressOut.flush();
        }
        if (this.signOut != null) {
            this.signOut.flush();
        }
        if (this.paddingOut != null) {
            this.paddingOut.flush();
        }
        if (this.encryptOut != null) {
            this.encryptOut.flush();
        }
        this.encodeOut.flush();
        if (this.armorOut != null) {
            this.armorOut.flush();
        }
        this.baseOut.flush();
    }

    @Override // java.io.OutputStream
    public void write(int i) throws IOException {
        this.plaintextOut.write(i);
    }

    @Override // java.io.OutputStream
    public void write(byte[] bArr) throws IOException {
        write(bArr, 0, bArr.length);
    }

    @Override // java.io.OutputStream
    public void write(byte[] bArr, int i, int i2) throws IOException {
        this.plaintextOut.write(bArr, i, i2);
    }
}
