package org.bouncycastle.asn1;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class BEROctetStringGenerator extends org.bouncycastle.asn1.BERGenerator {

    private class BufferedBEROctetStream extends java.io.OutputStream {
        private byte[] a;
        private int b;
        private org.bouncycastle.asn1.DEROutputStream c;
        final /* synthetic */ org.bouncycastle.asn1.BEROctetStringGenerator d;

        @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws java.io.IOException {
            int i = this.b;
            if (i != 0) {
                org.bouncycastle.asn1.DEROctetString.D(this.c, true, this.a, 0, i);
            }
            this.c.c();
            this.d.b();
        }

        @Override // java.io.OutputStream
        public void write(int i) throws java.io.IOException {
            byte[] bArr = this.a;
            int i2 = this.b;
            int i3 = i2 + 1;
            this.b = i3;
            bArr[i2] = (byte) i;
            if (i3 == bArr.length) {
                org.bouncycastle.asn1.DEROctetString.D(this.c, true, bArr, 0, bArr.length);
                this.b = 0;
            }
        }

        @Override // java.io.OutputStream
        public void write(byte[] bArr, int i, int i2) throws java.io.IOException {
            byte[] bArr2 = this.a;
            int length = bArr2.length;
            int i3 = this.b;
            int i4 = length - i3;
            if (i2 < i4) {
                java.lang.System.arraycopy(bArr, i, bArr2, i3, i2);
                this.b += i2;
                return;
            }
            if (i3 > 0) {
                java.lang.System.arraycopy(bArr, i, bArr2, i3, i4);
                org.bouncycastle.asn1.DEROctetString.D(this.c, true, this.a, 0, length);
            } else {
                i4 = 0;
            }
            while (true) {
                int i5 = i2 - i4;
                if (i5 < length) {
                    java.lang.System.arraycopy(bArr, i + i4, this.a, 0, i5);
                    this.b = i5;
                    return;
                } else {
                    org.bouncycastle.asn1.DEROctetString.D(this.c, true, bArr, i + i4, length);
                    i4 += length;
                }
            }
        }
    }
}
