package okio;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000L\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u0003\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0010\u000b\n\u0002\b\u0003\u0018\u00002\u00020\u0001J\u001f\u0010\u0007\u001a\u00020\u00062\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0007\u0010\bJ\u0011\u0010\n\u001a\u0004\u0018\u00010\tH\u0002¢\u0006\u0004\b\n\u0010\u000bJ\u001f\u0010\u000e\u001a\u00020\r2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\f\u001a\u00020\u0004H\u0016¢\u0006\u0004\b\u000e\u0010\u000fJ\u000f\u0010\u0010\u001a\u00020\rH\u0016¢\u0006\u0004\b\u0010\u0010\u0011J\u000f\u0010\u0013\u001a\u00020\u0012H\u0016¢\u0006\u0004\b\u0013\u0010\u0014J\u000f\u0010\u0015\u001a\u00020\rH\u0016¢\u0006\u0004\b\u0015\u0010\u0011R\u0014\u0010\u0018\u001a\u00020\u00168\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\n\u0010\u0017R\u0017\u0010\u001e\u001a\u00020\u00198\u0006¢\u0006\f\n\u0004\b\u001a\u0010\u001b\u001a\u0004\b\u001c\u0010\u001dR\u0014\u0010!\u001a\u00020\u00068\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u001f\u0010 R\u0016\u0010$\u001a\u00020\"8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0007\u0010#¨\u0006%"}, d2 = {"Lokio/CipherSink;", "Lokio/Sink;", "Lokio/Buffer;", "source", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "remaining", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "d", "(Lokio/Buffer;J)I", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "a", "()Ljava/lang/Throwable;", "byteCount", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "write", "(Lokio/Buffer;J)V", "flush", "()V", "Lokio/Timeout;", "timeout", "()Lokio/Timeout;", "close", "Lokio/BufferedSink;", "Lokio/BufferedSink;", "sink", "Ljavax/crypto/Cipher;", "b", "Ljavax/crypto/Cipher;", "getCipher", "()Ljavax/crypto/Cipher;", "cipher", "c", "I", "blockSize", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Z", "closed", "okio"}, k = 1, mv = {1, 9, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class CipherSink implements okio.Sink {

    /* JADX INFO: renamed from: a, reason: from kotlin metadata */
    private final okio.BufferedSink sink;

    /* JADX INFO: renamed from: b, reason: from kotlin metadata */
    private final javax.crypto.Cipher cipher;

    /* JADX INFO: renamed from: c, reason: from kotlin metadata */
    private final int blockSize;

    /* JADX INFO: renamed from: d, reason: from kotlin metadata */
    private boolean closed;

    private final java.lang.Throwable a() {
        int outputSize = this.cipher.getOutputSize(0);
        java.lang.Throwable th = null;
        if (outputSize == 0) {
            return null;
        }
        if (outputSize > 8192) {
            try {
                okio.BufferedSink bufferedSink = this.sink;
                byte[] bArrDoFinal = this.cipher.doFinal();
                kotlin.jvm.internal.Intrinsics.k(bArrDoFinal, "doFinal(...)");
                bufferedSink.write(bArrDoFinal);
                return null;
            } catch (java.lang.Throwable th2) {
                return th2;
            }
        }
        okio.Buffer bufferG = this.sink.g();
        okio.Segment segmentZ0 = bufferG.z0(outputSize);
        try {
            int iDoFinal = this.cipher.doFinal(segmentZ0.data, segmentZ0.limit);
            segmentZ0.limit += iDoFinal;
            bufferG.n0(bufferG.getSize() + ((long) iDoFinal));
        } catch (java.lang.Throwable th3) {
            th = th3;
        }
        if (segmentZ0.pos == segmentZ0.limit) {
            bufferG.head = segmentZ0.b();
            okio.SegmentPool.b(segmentZ0);
        }
        return th;
    }

    private final int d(okio.Buffer source, long remaining) throws javax.crypto.ShortBufferException {
        okio.Segment segment = source.head;
        kotlin.jvm.internal.Intrinsics.i(segment);
        int iMin = (int) java.lang.Math.min(remaining, segment.limit - segment.pos);
        okio.Buffer bufferG = this.sink.g();
        int outputSize = this.cipher.getOutputSize(iMin);
        int i = iMin;
        while (outputSize > 8192) {
            int i2 = this.blockSize;
            if (i <= i2) {
                okio.BufferedSink bufferedSink = this.sink;
                byte[] bArrUpdate = this.cipher.update(source.V1(remaining));
                kotlin.jvm.internal.Intrinsics.k(bArrUpdate, "update(...)");
                bufferedSink.write(bArrUpdate);
                return (int) remaining;
            }
            i -= i2;
            outputSize = this.cipher.getOutputSize(i);
        }
        okio.Segment segmentZ0 = bufferG.z0(outputSize);
        int iUpdate = this.cipher.update(segment.data, segment.pos, i, segmentZ0.data, segmentZ0.limit);
        segmentZ0.limit += iUpdate;
        bufferG.n0(bufferG.getSize() + ((long) iUpdate));
        if (segmentZ0.pos == segmentZ0.limit) {
            bufferG.head = segmentZ0.b();
            okio.SegmentPool.b(segmentZ0);
        }
        this.sink.t1();
        source.n0(source.getSize() - ((long) i));
        int i5 = segment.pos + i;
        segment.pos = i5;
        if (i5 == segment.limit) {
            source.head = segment.b();
            okio.SegmentPool.b(segment);
        }
        return i;
    }

    @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws java.lang.Throwable {
        if (this.closed) {
            return;
        }
        this.closed = true;
        java.lang.Throwable thA = a();
        try {
            this.sink.close();
        } catch (java.lang.Throwable th) {
            if (thA == null) {
                thA = th;
            }
        }
        if (thA != null) {
            throw thA;
        }
    }

    @Override // okio.Sink, java.io.Flushable
    public void flush() {
        this.sink.flush();
    }

    @Override // okio.Sink
    /* JADX INFO: renamed from: timeout */
    public okio.Timeout getTimeout() {
        return this.sink.getTimeout();
    }

    @Override // okio.Sink
    public void write(okio.Buffer source, long byteCount) {
        kotlin.jvm.internal.Intrinsics.l(source, "source");
        okio.SegmentedByteString.b(source.getSize(), 0L, byteCount);
        if (this.closed) {
            throw new java.lang.IllegalStateException("closed");
        }
        long jD = byteCount;
        while (jD > 0) {
            jD -= (long) d(source, jD);
        }
    }
}
