package com.facebook.imagepipeline.memory;

import X.C3088QFp;
import X.LwK;
import X.PHP;
import X.QG7;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.Closeable;
import java.nio.ByteBuffer;

public class NativeMemoryChunk implements C3088QFp, Closeable {
    public boolean mIsClosed;
    public final long mNativePtr;
    public final int mSize;

    public static native long nativeAllocate(int i);

    public static native void nativeCopyFromByteArray(long j, byte[] bArr, int i, int i2);

    public static native void nativeCopyToByteArray(long j, byte[] bArr, int i, int i2);

    public static native void nativeFree(long j);

    public static native void nativeMemcpy(long j, long j2, int i);

    public static native byte nativeReadByte(long j);

    public ByteBuffer getByteBuffer() {
        return null;
    }

    public long getNativePtr() {
        return this.mNativePtr;
    }

    public int getSize() {
        return this.mSize;
    }

    public long getUniqueId() {
        return this.mNativePtr;
    }

    public NativeMemoryChunk() {
        this.mIsClosed = true;
    }

    static {
        Covode.recordClassIndex(41661);
        LwK.LIZ("imagepipeline");
    }

    public synchronized boolean isClosed() {
        boolean z;
        MethodCollector.i(14908);
        z = this.mIsClosed;
        MethodCollector.o(14908);
        return z;
    }

    public synchronized void close() {
        MethodCollector.i(14907);
        if (!this.mIsClosed) {
            this.mIsClosed = true;
            nativeFree(this.mNativePtr);
        }
        MethodCollector.o(14907);
    }

    public void finalize() {
        if (!isClosed()) {
            try {
                close();
            } finally {
                super.finalize();
            }
        }
    }

    public NativeMemoryChunk(int i) {
        boolean z;
        MethodCollector.i(14906);
        if (i > 0) {
            z = true;
        } else {
            z = false;
        }
        PHP.LIZ(z);
        this.mSize = i;
        this.mNativePtr = nativeAllocate(i);
        this.mIsClosed = false;
        MethodCollector.o(14906);
    }

    public synchronized byte read(int i) {
        boolean z;
        boolean z2;
        byte nativeReadByte;
        MethodCollector.i(15483);
        boolean z3 = true;
        if (!isClosed()) {
            z = true;
        } else {
            z = false;
        }
        PHP.LIZIZ(z);
        if (i >= 0) {
            z2 = true;
        } else {
            z2 = false;
        }
        PHP.LIZ(z2);
        if (i >= this.mSize) {
            z3 = false;
        }
        PHP.LIZ(z3);
        nativeReadByte = nativeReadByte(this.mNativePtr + ((long) i));
        MethodCollector.o(15483);
        return nativeReadByte;
    }

    private void doCopy(int i, C3088QFp qFp, int i2, int i3) {
        MethodCollector.i(16219);
        if (qFp instanceof NativeMemoryChunk) {
            PHP.LIZIZ(!isClosed());
            PHP.LIZIZ(!qFp.isClosed());
            QG7.LIZ(i, qFp.getSize(), i2, i3, this.mSize);
            nativeMemcpy(qFp.getNativePtr() + ((long) i2), this.mNativePtr + ((long) i), i3);
            MethodCollector.o(16219);
            return;
        }
        IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Cannot copy two incompatible MemoryChunks");
        MethodCollector.o(16219);
        throw illegalArgumentException;
    }

    public synchronized int write(int i, byte[] bArr, int i2, int i3) {
        boolean z;
        int LIZ;
        MethodCollector.i(15110);
        PHP.LIZ(bArr);
        if (!isClosed()) {
            z = true;
        } else {
            z = false;
        }
        PHP.LIZIZ(z);
        LIZ = QG7.LIZ(i, i3, this.mSize);
        QG7.LIZ(i, bArr.length, i2, LIZ, this.mSize);
        nativeCopyFromByteArray(this.mNativePtr + ((long) i), bArr, i2, LIZ);
        MethodCollector.o(15110);
        return LIZ;
    }

    public void copy(int i, C3088QFp qFp, int i2, int i3) {
        MethodCollector.i(15864);
        PHP.LIZ(qFp);
        if (qFp.getUniqueId() == getUniqueId()) {
            PHP.LIZ(false);
        }
        if (qFp.getUniqueId() < getUniqueId()) {
            synchronized (qFp) {
                try {
                    synchronized (this) {
                        doCopy(i, qFp, i2, i3);
                    }
                } catch (Throwable th) {
                    MethodCollector.o(15864);
                    throw th;
                }
            }
            MethodCollector.o(15864);
            return;
        }
        synchronized (this) {
            try {
                synchronized (qFp) {
                    doCopy(i, qFp, i2, i3);
                }
            } catch (Throwable th2) {
                MethodCollector.o(15864);
                throw th2;
            }
        }
        MethodCollector.o(15864);
    }

    public synchronized int read(int i, byte[] bArr, int i2, int i3) {
        boolean z;
        int LIZ;
        MethodCollector.i(15311);
        PHP.LIZ(bArr);
        if (!isClosed()) {
            z = true;
        } else {
            z = false;
        }
        PHP.LIZIZ(z);
        LIZ = QG7.LIZ(i, i3, this.mSize);
        QG7.LIZ(i, bArr.length, i2, LIZ, this.mSize);
        nativeCopyToByteArray(this.mNativePtr + ((long) i), bArr, i2, LIZ);
        MethodCollector.o(15311);
        return LIZ;
    }
}
