package okio;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000T\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\b\n\u0002\b\u0011\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\b\b&\u0018\u00002\u00060\u0001j\u0002`\u0002:\u000289B\u000f\u0012\u0006\u0010\u0004\u001a\u00020\u0003¢\u0006\u0004\b\u0005\u0010\u0006J'\u0010\f\u001a\u00020\u00072\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\n\u001a\u00020\t2\u0006\u0010\u000b\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\f\u0010\rJ'\u0010\u0010\u001a\u00020\u000f2\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\u000e\u001a\u00020\t2\u0006\u0010\u000b\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\u0010\u0010\u0011J\r\u0010\u0012\u001a\u00020\u0007¢\u0006\u0004\b\u0012\u0010\u0013J\r\u0010\u0014\u001a\u00020\u000f¢\u0006\u0004\b\u0014\u0010\u0015J\u0017\u0010\u0017\u001a\u00020\u00162\b\b\u0002\u0010\b\u001a\u00020\u0007¢\u0006\u0004\b\u0017\u0010\u0018J\u0017\u0010\u001a\u001a\u00020\u00192\b\b\u0002\u0010\b\u001a\u00020\u0007¢\u0006\u0004\b\u001a\u0010\u001bJ\r\u0010\u001c\u001a\u00020\u000f¢\u0006\u0004\b\u001c\u0010\u0015J/\u0010!\u001a\u00020\u001f2\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\u001e\u001a\u00020\u001d2\u0006\u0010 \u001a\u00020\u001f2\u0006\u0010\u000b\u001a\u00020\u001fH$¢\u0006\u0004\b!\u0010\"J/\u0010#\u001a\u00020\u000f2\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\u001e\u001a\u00020\u001d2\u0006\u0010 \u001a\u00020\u001f2\u0006\u0010\u000b\u001a\u00020\u001fH$¢\u0006\u0004\b#\u0010$J\u000f\u0010%\u001a\u00020\u000fH$¢\u0006\u0004\b%\u0010\u0015J\u000f\u0010&\u001a\u00020\u0007H$¢\u0006\u0004\b&\u0010\u0013J\u000f\u0010'\u001a\u00020\u000fH$¢\u0006\u0004\b'\u0010\u0015R\u0017\u0010\u0004\u001a\u00020\u00038\u0006¢\u0006\f\n\u0004\b(\u0010)\u001a\u0004\b*\u0010+R\u0016\u0010-\u001a\u00020\u00038\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b,\u0010)R\u0016\u00100\u001a\u00020\u001f8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b.\u0010/R\u001b\u00107\u001a\u000601j\u0002`28\u0006¢\u0006\f\n\u0004\b3\u00104\u001a\u0004\b5\u00106¨\u0006:"}, d2 = {"Lokio/FileHandle;", "Ljava/io/Closeable;", "Lokio/Closeable;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "readWrite", "<init>", "(Z)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "fileOffset", "Lokio/Buffer;", "sink", "byteCount", "G", "(JLokio/Buffer;J)J", "source", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Y", "(JLokio/Buffer;J)V", "size", "()J", "flush", "()V", "Lokio/Source;", "X", "(J)Lokio/Source;", "Lokio/Sink;", "O", "(J)Lokio/Sink;", "close", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "array", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "arrayOffset", "y", "(J[BII)I", "E", "(J[BII)V", "v", "C", "u", "a", "Z", "getReadWrite", "()Z", "b", "closed", "c", "I", "openStreamCount", "Ljava/util/concurrent/locks/ReentrantLock;", "Lokio/Lock;", "d", "Ljava/util/concurrent/locks/ReentrantLock;", "r", "()Ljava/util/concurrent/locks/ReentrantLock;", "lock", "FileHandleSink", "FileHandleSource", "okio"}, k = 1, mv = {1, 9, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public abstract class FileHandle implements java.io.Closeable {

    /* JADX INFO: renamed from: a, reason: from kotlin metadata */
    private final boolean readWrite;

    /* JADX INFO: renamed from: b, reason: from kotlin metadata */
    private boolean closed;

    /* JADX INFO: renamed from: c, reason: from kotlin metadata */
    private int openStreamCount;

    /* JADX INFO: renamed from: d, reason: from kotlin metadata */
    private final java.util.concurrent.locks.ReentrantLock lock = okio._JvmPlatformKt.b();

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\r\n\u0002\u0010\u000b\n\u0002\b\b\b\u0002\u0018\u00002\u00020\u0001B\u0017\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007J\u001f\u0010\f\u001a\u00020\u000b2\u0006\u0010\t\u001a\u00020\b2\u0006\u0010\n\u001a\u00020\u0004H\u0016¢\u0006\u0004\b\f\u0010\rJ\u000f\u0010\u000e\u001a\u00020\u000bH\u0016¢\u0006\u0004\b\u000e\u0010\u000fJ\u000f\u0010\u0011\u001a\u00020\u0010H\u0016¢\u0006\u0004\b\u0011\u0010\u0012J\u000f\u0010\u0013\u001a\u00020\u000bH\u0016¢\u0006\u0004\b\u0013\u0010\u000fR\u0017\u0010\u0003\u001a\u00020\u00028\u0006¢\u0006\f\n\u0004\b\u0014\u0010\u0015\u001a\u0004\b\u0016\u0010\u0017R\"\u0010\u0005\u001a\u00020\u00048\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u0018\u0010\u0019\u001a\u0004\b\u001a\u0010\u001b\"\u0004\b\u001c\u0010\u001dR\"\u0010%\u001a\u00020\u001e8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u001f\u0010 \u001a\u0004\b!\u0010\"\"\u0004\b#\u0010$¨\u0006&"}, d2 = {"Lokio/FileHandle$FileHandleSink;", "Lokio/Sink;", "Lokio/FileHandle;", "fileHandle", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "position", "<init>", "(Lokio/FileHandle;J)V", "Lokio/Buffer;", "source", "byteCount", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "write", "(Lokio/Buffer;J)V", "flush", "()V", "Lokio/Timeout;", "timeout", "()Lokio/Timeout;", "close", "a", "Lokio/FileHandle;", "getFileHandle", "()Lokio/FileHandle;", "b", "J", "getPosition", "()J", "setPosition", "(J)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "c", "Z", "getClosed", "()Z", "setClosed", "(Z)V", "closed", "okio"}, k = 1, mv = {1, 9, 0}, xi = 48)
    @kotlin.jvm.internal.SourceDebugExtension
    private static final class FileHandleSink implements okio.Sink {

        /* JADX INFO: renamed from: a, reason: from kotlin metadata */
        private final okio.FileHandle fileHandle;

        /* JADX INFO: renamed from: b, reason: from kotlin metadata */
        private long position;

        /* JADX INFO: renamed from: c, reason: from kotlin metadata */
        private boolean closed;

        public FileHandleSink(okio.FileHandle fileHandle, long j) {
            kotlin.jvm.internal.Intrinsics.l(fileHandle, "fileHandle");
            this.fileHandle = fileHandle;
            this.position = j;
        }

        @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.closed) {
                return;
            }
            this.closed = true;
            java.util.concurrent.locks.ReentrantLock lock = this.fileHandle.getLock();
            lock.lock();
            try {
                this.fileHandle.openStreamCount--;
                if (this.fileHandle.openStreamCount == 0 && this.fileHandle.closed) {
                    kotlin.Unit unit = kotlin.Unit.a;
                    lock.unlock();
                    this.fileHandle.u();
                    return;
                }
                lock.unlock();
            } catch (java.lang.Throwable th) {
                lock.unlock();
                throw th;
            }
        }

        @Override // okio.Sink, java.io.Flushable
        public void flush() {
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            this.fileHandle.v();
        }

        @Override // okio.Sink
        public okio.Timeout timeout() {
            return okio.Timeout.NONE;
        }

        @Override // okio.Sink
        public void write(okio.Buffer source, long byteCount) {
            kotlin.jvm.internal.Intrinsics.l(source, "source");
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            this.fileHandle.Y(this.position, source, byteCount);
            this.position += byteCount;
        }
    }

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\f\n\u0002\u0010\u000b\n\u0002\b\b\b\u0002\u0018\u00002\u00020\u0001B\u0017\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007J\u001f\u0010\u000b\u001a\u00020\u00042\u0006\u0010\t\u001a\u00020\b2\u0006\u0010\n\u001a\u00020\u0004H\u0016¢\u0006\u0004\b\u000b\u0010\fJ\u000f\u0010\u000e\u001a\u00020\rH\u0016¢\u0006\u0004\b\u000e\u0010\u000fJ\u000f\u0010\u0011\u001a\u00020\u0010H\u0016¢\u0006\u0004\b\u0011\u0010\u0012R\u0017\u0010\u0003\u001a\u00020\u00028\u0006¢\u0006\f\n\u0004\b\u0013\u0010\u0014\u001a\u0004\b\u0015\u0010\u0016R\"\u0010\u0005\u001a\u00020\u00048\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u0017\u0010\u0018\u001a\u0004\b\u0019\u0010\u001a\"\u0004\b\u001b\u0010\u001cR\"\u0010$\u001a\u00020\u001d8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u001e\u0010\u001f\u001a\u0004\b \u0010!\"\u0004\b\"\u0010#¨\u0006%"}, d2 = {"Lokio/FileHandle$FileHandleSource;", "Lokio/Source;", "Lokio/FileHandle;", "fileHandle", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "position", "<init>", "(Lokio/FileHandle;J)V", "Lokio/Buffer;", "sink", "byteCount", "read", "(Lokio/Buffer;J)J", "Lokio/Timeout;", "timeout", "()Lokio/Timeout;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "close", "()V", "a", "Lokio/FileHandle;", "getFileHandle", "()Lokio/FileHandle;", "b", "J", "getPosition", "()J", "setPosition", "(J)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "c", "Z", "getClosed", "()Z", "setClosed", "(Z)V", "closed", "okio"}, k = 1, mv = {1, 9, 0}, xi = 48)
    @kotlin.jvm.internal.SourceDebugExtension
    private static final class FileHandleSource implements okio.Source {

        /* JADX INFO: renamed from: a, reason: from kotlin metadata */
        private final okio.FileHandle fileHandle;

        /* JADX INFO: renamed from: b, reason: from kotlin metadata */
        private long position;

        /* JADX INFO: renamed from: c, reason: from kotlin metadata */
        private boolean closed;

        public FileHandleSource(okio.FileHandle fileHandle, long j) {
            kotlin.jvm.internal.Intrinsics.l(fileHandle, "fileHandle");
            this.fileHandle = fileHandle;
            this.position = j;
        }

        @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.closed) {
                return;
            }
            this.closed = true;
            java.util.concurrent.locks.ReentrantLock lock = this.fileHandle.getLock();
            lock.lock();
            try {
                this.fileHandle.openStreamCount--;
                if (this.fileHandle.openStreamCount == 0 && this.fileHandle.closed) {
                    kotlin.Unit unit = kotlin.Unit.a;
                    lock.unlock();
                    this.fileHandle.u();
                    return;
                }
                lock.unlock();
            } catch (java.lang.Throwable th) {
                lock.unlock();
                throw th;
            }
        }

        @Override // okio.Source
        public long read(okio.Buffer sink, long byteCount) {
            kotlin.jvm.internal.Intrinsics.l(sink, "sink");
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            long jG = this.fileHandle.G(this.position, sink, byteCount);
            if (jG != -1) {
                this.position += jG;
            }
            return jG;
        }

        @Override // okio.Source
        public okio.Timeout timeout() {
            return okio.Timeout.NONE;
        }
    }

    public FileHandle(boolean z) {
        this.readWrite = z;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long G(long fileOffset, okio.Buffer sink, long byteCount) {
        if (byteCount < 0) {
            throw new java.lang.IllegalArgumentException(("byteCount < 0: " + byteCount).toString());
        }
        long j = byteCount + fileOffset;
        long j2 = fileOffset;
        while (j2 < j) {
            okio.Segment segmentZ0 = sink.z0(1);
            byte[] bArr = segmentZ0.data;
            int i = segmentZ0.limit;
            int iY = y(j2, bArr, i, (int) java.lang.Math.min(j - j2, 8192 - i));
            if (iY == -1) {
                if (segmentZ0.pos == segmentZ0.limit) {
                    sink.head = segmentZ0.b();
                    okio.SegmentPool.b(segmentZ0);
                }
                if (fileOffset != j2) {
                    break;
                }
                return -1L;
            }
            segmentZ0.limit += iY;
            long j5 = iY;
            j2 += j5;
            sink.n0(sink.getSize() + j5);
        }
        return j2 - fileOffset;
    }

    public static /* synthetic */ okio.Sink R(okio.FileHandle fileHandle, long j, int i, java.lang.Object obj) {
        if (obj != null) {
            throw new java.lang.UnsupportedOperationException("Super calls with default arguments not supported in this target, function: sink");
        }
        if ((i & 1) != 0) {
            j = 0;
        }
        return fileHandle.O(j);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void Y(long fileOffset, okio.Buffer source, long byteCount) {
        okio.SegmentedByteString.b(source.getSize(), 0L, byteCount);
        long j = fileOffset + byteCount;
        long j2 = fileOffset;
        while (j2 < j) {
            okio.Segment segment = source.head;
            kotlin.jvm.internal.Intrinsics.i(segment);
            int iMin = (int) java.lang.Math.min(j - j2, segment.limit - segment.pos);
            okio.FileHandle fileHandle = this;
            fileHandle.E(j2, segment.data, segment.pos, iMin);
            segment.pos += iMin;
            long j5 = iMin;
            j2 += j5;
            source.n0(source.getSize() - j5);
            if (segment.pos == segment.limit) {
                source.head = segment.b();
                okio.SegmentPool.b(segment);
            }
            this = fileHandle;
        }
    }

    protected abstract long C();

    protected abstract void E(long fileOffset, byte[] array, int arrayOffset, int byteCount);

    public final okio.Sink O(long fileOffset) {
        if (!this.readWrite) {
            throw new java.lang.IllegalStateException("file handle is read-only");
        }
        java.util.concurrent.locks.ReentrantLock reentrantLock = this.lock;
        reentrantLock.lock();
        try {
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            this.openStreamCount++;
            reentrantLock.unlock();
            return new okio.FileHandle.FileHandleSink(this, fileOffset);
        } catch (java.lang.Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    public final okio.Source X(long fileOffset) {
        java.util.concurrent.locks.ReentrantLock reentrantLock = this.lock;
        reentrantLock.lock();
        try {
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            this.openStreamCount++;
            reentrantLock.unlock();
            return new okio.FileHandle.FileHandleSource(this, fileOffset);
        } catch (java.lang.Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        java.util.concurrent.locks.ReentrantLock reentrantLock = this.lock;
        reentrantLock.lock();
        try {
            if (this.closed) {
                reentrantLock.unlock();
                return;
            }
            this.closed = true;
            if (this.openStreamCount != 0) {
                reentrantLock.unlock();
                return;
            }
            kotlin.Unit unit = kotlin.Unit.a;
            reentrantLock.unlock();
            u();
        } catch (java.lang.Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    public final void flush() {
        if (!this.readWrite) {
            throw new java.lang.IllegalStateException("file handle is read-only");
        }
        java.util.concurrent.locks.ReentrantLock reentrantLock = this.lock;
        reentrantLock.lock();
        try {
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            kotlin.Unit unit = kotlin.Unit.a;
            reentrantLock.unlock();
            v();
        } catch (java.lang.Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    /* JADX INFO: renamed from: r, reason: from getter */
    public final java.util.concurrent.locks.ReentrantLock getLock() {
        return this.lock;
    }

    public final long size() {
        java.util.concurrent.locks.ReentrantLock reentrantLock = this.lock;
        reentrantLock.lock();
        try {
            if (this.closed) {
                throw new java.lang.IllegalStateException("closed");
            }
            kotlin.Unit unit = kotlin.Unit.a;
            reentrantLock.unlock();
            return C();
        } catch (java.lang.Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    protected abstract void u();

    protected abstract void v();

    protected abstract int y(long fileOffset, byte[] array, int arrayOffset, int byteCount);
}
