package p138.p145.p146.p174;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: renamed from: ދ.ؠ.֏.ޒ.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C3227 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static final AtomicReference<byte[]> f9566 = new AtomicReference<>();

    /* JADX INFO: renamed from: ދ.ؠ.֏.ޒ.֏$֏, reason: contains not printable characters */
    public static class C3228 extends InputStream {

        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public final ByteBuffer f9567;

        /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
        public int f9568 = -1;

        public C3228(ByteBuffer byteBuffer) {
            this.f9567 = byteBuffer;
        }

        @Override // java.io.InputStream
        public int available() {
            return this.f9567.remaining();
        }

        @Override // java.io.InputStream
        public synchronized void mark(int i) {
            this.f9568 = this.f9567.position();
        }

        @Override // java.io.InputStream
        public boolean markSupported() {
            return true;
        }

        @Override // java.io.InputStream
        public int read() {
            if (this.f9567.hasRemaining()) {
                return this.f9567.get() & 255;
            }
            return -1;
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) {
            if (!this.f9567.hasRemaining()) {
                return -1;
            }
            int iMin = Math.min(i2, available());
            this.f9567.get(bArr, i, iMin);
            return iMin;
        }

        @Override // java.io.InputStream
        public synchronized void reset() {
            if (this.f9568 == -1) {
                throw new IOException("Cannot reset to unset mark position");
            }
            this.f9567.position(this.f9568);
        }

        @Override // java.io.InputStream
        public long skip(long j) {
            if (!this.f9567.hasRemaining()) {
                return -1L;
            }
            long jMin = Math.min(j, available());
            ByteBuffer byteBuffer = this.f9567;
            byteBuffer.position((int) (((long) byteBuffer.position()) + jMin));
            return jMin;
        }
    }

    /* JADX INFO: renamed from: ދ.ؠ.֏.ޒ.֏$ؠ, reason: contains not printable characters */
    public static final class C3229 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final int f9569;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final int f9570;

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public final byte[] f9571;

        public C3229(byte[] bArr, int i, int i2) {
            this.f9571 = bArr;
            this.f9569 = i;
            this.f9570 = i2;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static ByteBuffer m5311(File file) throws Throwable {
        RandomAccessFile randomAccessFile;
        FileChannel channel = null;
        try {
            long length = file.length();
            if (length > 2147483647L) {
                throw new IOException("File too large to map into memory");
            }
            if (length == 0) {
                throw new IOException("File unsuitable for memory mapping");
            }
            randomAccessFile = new RandomAccessFile(file, "r");
            try {
                channel = randomAccessFile.getChannel();
                MappedByteBuffer mappedByteBufferLoad = channel.map(FileChannel.MapMode.READ_ONLY, 0L, length).load();
                try {
                    channel.close();
                } catch (IOException unused) {
                }
                try {
                    randomAccessFile.close();
                } catch (IOException unused2) {
                }
                return mappedByteBufferLoad;
            } catch (Throwable th) {
                th = th;
                if (channel != null) {
                    try {
                        channel.close();
                    } catch (IOException unused3) {
                    }
                }
                if (randomAccessFile == null) {
                    throw th;
                }
                try {
                    randomAccessFile.close();
                    throw th;
                } catch (IOException unused4) {
                    throw th;
                }
            }
        } catch (Throwable th2) {
            th = th2;
            randomAccessFile = null;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static ByteBuffer m5312(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(16384);
        byte[] andSet = f9566.getAndSet(null);
        if (andSet == null) {
            andSet = new byte[16384];
        }
        while (true) {
            int i = inputStream.read(andSet);
            if (i < 0) {
                f9566.set(andSet);
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                return (ByteBuffer) ByteBuffer.allocateDirect(byteArray.length).put(byteArray).position(0);
            }
            byteArrayOutputStream.write(andSet, 0, i);
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static void m5313(ByteBuffer byteBuffer, File file) throws Throwable {
        RandomAccessFile randomAccessFile;
        byteBuffer.position(0);
        FileChannel channel = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            try {
                channel = randomAccessFile.getChannel();
                channel.write(byteBuffer);
                channel.force(false);
                channel.close();
                randomAccessFile.close();
                try {
                    channel.close();
                } catch (IOException unused) {
                }
                try {
                    randomAccessFile.close();
                } catch (IOException unused2) {
                }
            } catch (Throwable th) {
                th = th;
                if (channel != null) {
                    try {
                        channel.close();
                    } catch (IOException unused3) {
                    }
                }
                if (randomAccessFile == null) {
                    throw th;
                }
                try {
                    randomAccessFile.close();
                    throw th;
                } catch (IOException unused4) {
                    throw th;
                }
            }
        } catch (Throwable th2) {
            th = th2;
            randomAccessFile = null;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static byte[] m5314(ByteBuffer byteBuffer) {
        C3229 c3229 = (byteBuffer.isReadOnly() || !byteBuffer.hasArray()) ? null : new C3229(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
        if (c3229 != null && c3229.f9569 == 0 && c3229.f9570 == c3229.f9571.length) {
            return byteBuffer.array();
        }
        ByteBuffer byteBufferAsReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        byte[] bArr = new byte[byteBufferAsReadOnlyBuffer.limit()];
        byteBufferAsReadOnlyBuffer.position(0);
        byteBufferAsReadOnlyBuffer.get(bArr);
        return bArr;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public static InputStream m5315(ByteBuffer byteBuffer) {
        return new C3228(byteBuffer);
    }
}
