package org.bouncycastle.crypto.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.StreamCipher;
import org.bouncycastle.crypto.modes.AEADBlockCipher;

/* JADX INFO: loaded from: classes3.dex */
public class CipherOutputStream extends FilterOutputStream {
    private AEADBlockCipher aeadBlockCipher;
    private byte[] buf;
    private BufferedBlockCipher bufferedBlockCipher;
    private final byte[] oneByte;
    private StreamCipher streamCipher;

    public CipherOutputStream(OutputStream outputStream, BufferedBlockCipher bufferedBlockCipher) {
        super(outputStream);
        this.oneByte = new byte[1];
        this.bufferedBlockCipher = bufferedBlockCipher;
    }

    public CipherOutputStream(OutputStream outputStream, StreamCipher streamCipher) {
        super(outputStream);
        this.oneByte = new byte[1];
        this.streamCipher = streamCipher;
    }

    public CipherOutputStream(OutputStream outputStream, AEADBlockCipher aEADBlockCipher) {
        super(outputStream);
        this.oneByte = new byte[1];
        this.aeadBlockCipher = aEADBlockCipher;
    }

    private void ensureCapacity(int i, boolean z) {
        BufferedBlockCipher bufferedBlockCipher = this.bufferedBlockCipher;
        if (z) {
            if (bufferedBlockCipher != null) {
                i = this.bufferedBlockCipher.getOutputSize(i);
            } else if (this.aeadBlockCipher != null) {
                i = this.aeadBlockCipher.getOutputSize(i);
            }
        } else if (bufferedBlockCipher != null) {
            i = this.bufferedBlockCipher.getUpdateOutputSize(i);
        } else if (this.aeadBlockCipher != null) {
            i = this.aeadBlockCipher.getUpdateOutputSize(i);
        }
        if (this.buf == null || this.buf.length < i) {
            this.buf = new byte[i];
        }
    }

    @Override // java.io.FilterOutputStream, java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        IOException cipherIOException;
        IOException iOException;
        ensureCapacity(0, true);
        try {
            if (this.bufferedBlockCipher != null) {
                int iDoFinal = this.bufferedBlockCipher.doFinal(this.buf, 0);
                if (iDoFinal != 0) {
                    this.out.write(this.buf, 0, iDoFinal);
                }
            } else if (this.aeadBlockCipher != null) {
                int iDoFinal2 = this.aeadBlockCipher.doFinal(this.buf, 0);
                if (iDoFinal2 != 0) {
                    this.out.write(this.buf, 0, iDoFinal2);
                }
            } else if (this.streamCipher != null) {
                this.streamCipher.reset();
            }
            iOException = null;
        } catch (InvalidCipherTextException e) {
            cipherIOException = new InvalidCipherTextIOException("Error finalising cipher data", e);
            iOException = cipherIOException;
        } catch (Exception e2) {
            cipherIOException = new CipherIOException("Error closing stream: ", e2);
            iOException = cipherIOException;
        }
        try {
            flush();
            this.out.close();
        } catch (IOException e3) {
            if (iOException == null) {
                iOException = e3;
            }
        }
        if (iOException != null) {
            throw iOException;
        }
    }

    @Override // java.io.FilterOutputStream, java.io.OutputStream, java.io.Flushable
    public void flush() throws IOException {
        this.out.flush();
    }

    @Override // java.io.FilterOutputStream, java.io.OutputStream
    public void write(int i) throws IOException {
        byte b = (byte) i;
        this.oneByte[0] = b;
        if (this.streamCipher != null) {
            this.out.write(this.streamCipher.returnByte(b));
        } else {
            write(this.oneByte, 0, 1);
        }
    }

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

    @Override // java.io.FilterOutputStream, java.io.OutputStream
    public void write(byte[] bArr, int i, int i2) throws IOException {
        ensureCapacity(i2, false);
        if (this.bufferedBlockCipher != null) {
            int iProcessBytes = this.bufferedBlockCipher.processBytes(bArr, i, i2, this.buf, 0);
            if (iProcessBytes != 0) {
                this.out.write(this.buf, 0, iProcessBytes);
                return;
            }
            return;
        }
        if (this.aeadBlockCipher == null) {
            this.streamCipher.processBytes(bArr, i, i2, this.buf, 0);
            this.out.write(this.buf, 0, i2);
        } else {
            int iProcessBytes2 = this.aeadBlockCipher.processBytes(bArr, i, i2, this.buf, 0);
            if (iProcessBytes2 != 0) {
                this.out.write(this.buf, 0, iProcessBytes2);
            }
        }
    }
}
