package org.bouncycastle.bcpg;

import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.asn1.BERTags;
import org.bouncycastle.asn1.cmc.BodyPartID;
import org.bouncycastle.asn1.cmp.PKIFailureInfo;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class BCPGOutputStream extends OutputStream implements PacketTags, CompressionAlgorithmTags {
    private static final int BUF_SIZE_POWER = 16;
    OutputStream out;
    private PacketFormat packetFormat;
    private byte[] partialBuffer;
    private int partialBufferLength;
    private int partialOffset;
    private int partialPower;

    public BCPGOutputStream(OutputStream outputStream) {
        this(outputStream, PacketFormat.ROUNDTRIP);
    }

    public BCPGOutputStream(OutputStream outputStream, int i) throws IOException {
        this.out = outputStream;
        this.packetFormat = PacketFormat.LEGACY;
        writeHeader(i, true, true, 0L);
    }

    public BCPGOutputStream(OutputStream outputStream, int i, long j) throws IOException {
        this.out = outputStream;
        this.packetFormat = PacketFormat.CURRENT;
        writeHeader(i, false, false, j);
    }

    public BCPGOutputStream(OutputStream outputStream, int i, long j, boolean z) throws IOException {
        this.out = outputStream;
        this.packetFormat = z ? PacketFormat.LEGACY : PacketFormat.CURRENT;
        if (j <= BodyPartID.bodyIdMax) {
            writeHeader(i, z, false, j);
            return;
        }
        writeHeader(i, false, true, 0L);
        this.partialBufferLength = PKIFailureInfo.notAuthorized;
        this.partialBuffer = new byte[this.partialBufferLength];
        this.partialPower = 16;
        this.partialOffset = 0;
    }

    public BCPGOutputStream(OutputStream outputStream, int i, byte[] bArr) throws IOException {
        int i2;
        this.out = outputStream;
        this.packetFormat = PacketFormat.CURRENT;
        writeHeader(i, false, true, 0L);
        this.partialBuffer = bArr;
        int length = this.partialBuffer.length;
        this.partialPower = 0;
        while (true) {
            i2 = this.partialPower;
            if (length == 1) {
                break;
            }
            length >>>= 1;
            this.partialPower = i2 + 1;
        }
        if (i2 > 30) {
            throw new IOException("Buffer cannot be greater than 2^30 in length.");
        }
        this.partialBufferLength = 1 << this.partialPower;
        this.partialOffset = 0;
    }

    public BCPGOutputStream(OutputStream outputStream, PacketFormat packetFormat) {
        this.out = outputStream;
        this.packetFormat = packetFormat;
    }

    public BCPGOutputStream(OutputStream outputStream, boolean z) {
        this(outputStream, z ? PacketFormat.CURRENT : PacketFormat.ROUNDTRIP);
    }

    private void partialFlush(boolean z) throws IOException {
        OutputStream outputStream = this.out;
        if (z) {
            StreamUtil.writeNewPacketLength(outputStream, this.partialOffset);
            this.out.write(this.partialBuffer, 0, this.partialOffset);
        } else {
            outputStream.write(this.partialPower | BERTags.FLAGS);
            this.out.write(this.partialBuffer, 0, this.partialBufferLength);
        }
        this.partialOffset = 0;
    }

    public static BCPGOutputStream wrap(OutputStream outputStream) {
        return outputStream instanceof BCPGOutputStream ? (BCPGOutputStream) outputStream : new BCPGOutputStream(outputStream);
    }

    private void writeHeader(int i, boolean z, boolean z2, long j) throws IOException {
        int i2;
        if (this.partialBuffer != null) {
            partialFlush(true);
            this.partialBuffer = null;
        }
        if (i > 15 || !z) {
            write(i | 64 | 128);
            if (z2) {
                this.partialOffset = 0;
                return;
            } else {
                StreamUtil.writeNewPacketLength(this.out, j);
                return;
            }
        }
        int i3 = (i << 2) | 128;
        if (z2) {
            i2 = i3 | 3;
        } else {
            if (j > 255) {
                if (j <= 65535) {
                    write(i3 | 1);
                    StreamUtil.write2OctetLength(this, (int) j);
                    return;
                } else {
                    write(i3 | 2);
                    StreamUtil.writeBodyLen(this, j);
                    return;
                }
            }
            write(i3);
            i2 = (byte) j;
        }
        write(i2);
    }

    private void writePartial(byte b) throws IOException {
        if (this.partialOffset == this.partialBufferLength) {
            partialFlush(false);
        }
        byte[] bArr = this.partialBuffer;
        int i = this.partialOffset;
        this.partialOffset = i + 1;
        bArr[i] = b;
    }

    private void writePartial(byte[] bArr, int i, int i2) throws IOException {
        byte[] bArr2;
        if (this.partialOffset == this.partialBufferLength) {
            partialFlush(false);
        }
        int i3 = this.partialBufferLength - this.partialOffset;
        byte[] bArr3 = this.partialBuffer;
        if (i2 <= i3) {
            System.arraycopy(bArr, i, bArr3, this.partialOffset, i2);
        } else {
            System.arraycopy(bArr, i, bArr3, this.partialOffset, this.partialBufferLength - this.partialOffset);
            int i4 = i + (this.partialBufferLength - this.partialOffset);
            int i5 = this.partialBufferLength - this.partialOffset;
            while (true) {
                i2 -= i5;
                partialFlush(false);
                int i6 = this.partialBufferLength;
                bArr2 = this.partialBuffer;
                if (i2 <= i6) {
                    break;
                }
                System.arraycopy(bArr, i4, bArr2, 0, this.partialBufferLength);
                i4 += this.partialBufferLength;
                i5 = this.partialBufferLength;
            }
            System.arraycopy(bArr, i4, bArr2, 0, i2);
        }
        this.partialOffset += i2;
    }

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

    public void finish() throws IOException {
        if (this.partialBuffer != null) {
            partialFlush(true);
            Arrays.fill(this.partialBuffer, (byte) 0);
            this.partialBuffer = null;
        }
    }

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

    @Override // java.io.OutputStream
    public void write(int i) throws IOException {
        if (this.partialBuffer != null) {
            writePartial((byte) i);
        } else {
            this.out.write(i);
        }
    }

    @Override // java.io.OutputStream
    public void write(byte[] bArr, int i, int i2) throws IOException {
        if (this.partialBuffer != null) {
            writePartial(bArr, i, i2);
        } else {
            this.out.write(bArr, i, i2);
        }
    }

    public void writeObject(BCPGObject bCPGObject) throws IOException {
        bCPGObject.encode(this);
    }

    void writePacket(int i, byte[] bArr) throws IOException {
        writeHeader(i, this.packetFormat == PacketFormat.LEGACY, false, bArr.length);
        write(bArr);
    }

    void writePacket(int i, byte[] bArr, boolean z) throws IOException {
        writeHeader(i, z, false, bArr.length);
        write(bArr);
    }

    public void writePacket(ContainedPacket containedPacket) throws IOException {
        containedPacket.encode(this);
    }

    void writePacket(boolean z, int i, byte[] bArr) throws IOException {
        writeHeader(i, this.packetFormat == PacketFormat.LEGACY || (this.packetFormat == PacketFormat.ROUNDTRIP && !z), false, bArr.length);
        write(bArr);
    }
}
