package org.bouncycastle.est;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
class CTEBase64InputStream extends java.io.InputStream {
    protected final java.io.InputStream a;
    protected final byte[] b;
    protected final byte[] c;
    protected final java.io.OutputStream d;
    protected final java.lang.Long e;
    protected int f;
    protected int g;
    protected long h;

    /* JADX INFO: renamed from: org.bouncycastle.est.CTEBase64InputStream$1, reason: invalid class name */
    class AnonymousClass1 extends java.io.OutputStream {
        final /* synthetic */ org.bouncycastle.est.CTEBase64InputStream a;

        @Override // java.io.OutputStream
        public void write(int i) {
            org.bouncycastle.est.CTEBase64InputStream cTEBase64InputStream = this.a;
            byte[] bArr = cTEBase64InputStream.c;
            int i2 = cTEBase64InputStream.g;
            cTEBase64InputStream.g = i2 + 1;
            bArr[i2] = (byte) i;
        }
    }

    protected int a() throws java.io.IOException {
        int i;
        int i2 = 0;
        do {
            java.lang.Long l = this.e;
            if (l != null && this.h > l.longValue()) {
                return -1;
            }
            i = this.a.read();
            if (i >= 33 || i == 13 || i == 10) {
                byte[] bArr = this.b;
                if (i2 >= bArr.length) {
                    throw new java.io.IOException("Content Transfer Encoding, base64 line length > 1024");
                }
                bArr[i2] = (byte) i;
                this.h++;
                i2++;
            } else if (i >= 0) {
                this.h++;
            }
            if (i <= -1 || i2 >= this.b.length) {
                break;
            }
        } while (i != 10);
        if (i2 > 0) {
            try {
                org.bouncycastle.util.encoders.Base64.a(this.b, 0, i2, this.d);
            } catch (java.lang.Exception e) {
                throw new java.io.IOException("Decode Base64 Content-Transfer-Encoding: " + e);
            }
        } else if (i == -1) {
            return -1;
        }
        return this.g;
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws java.io.IOException {
        this.a.close();
    }

    @Override // java.io.InputStream
    public int read() throws java.io.IOException {
        if (this.f == this.g) {
            this.f = 0;
            this.g = 0;
            int iA = a();
            if (iA == -1) {
                return iA;
            }
        }
        byte[] bArr = this.c;
        int i = this.f;
        this.f = i + 1;
        return bArr[i] & 255;
    }
}
