package ඓ.ῲ.K.ᓣ;

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;

/* renamed from: ඓ.ῲ.K.ᓣ.K  reason: contains not printable characters */
/* compiled from: ByteBufferUtil */
public final class C0842 {

    /* renamed from: K  reason: contains not printable characters */
    public static final AtomicReference<byte[]> f4064 = new AtomicReference<>();

    /* renamed from: ඓ.ῲ.K.ᓣ.K$ῲ  reason: contains not printable characters */
    /* compiled from: ByteBufferUtil */
    public static final class C0843 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final byte[] f4065;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final int f4066;

        /* renamed from: K  reason: contains not printable characters */
        public final int f4067;

        public C0843(byte[] bArr, int i, int i2) {
            this.f4065 = bArr;
            this.f4067 = i;
            this.f4066 = i2;
        }
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public static InputStream m2444(ByteBuffer byteBuffer) {
        return new C0844(byteBuffer);
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(7:9|10|11|12|13|14|15) */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:13:0x002d */
    /* JADX WARNING: Removed duplicated region for block: B:27:0x004c A[SYNTHETIC, Splitter:B:27:0x004c] */
    /* JADX WARNING: Removed duplicated region for block: B:31:0x0053 A[SYNTHETIC, Splitter:B:31:0x0053] */
    /* renamed from: K  reason: contains not printable characters */
    public static ByteBuffer m2445(File file) throws IOException {
        Throwable th;
        RandomAccessFile randomAccessFile;
        FileChannel fileChannel = null;
        try {
            long length = file.length();
            if (length > 2147483647L) {
                throw new IOException("File too large to map into memory");
            } else if (length != 0) {
                randomAccessFile = new RandomAccessFile(file, "r");
                try {
                    FileChannel channel = randomAccessFile.getChannel();
                    try {
                        MappedByteBuffer load = channel.map(FileChannel.MapMode.READ_ONLY, 0, length).load();
                        channel.close();
                        randomAccessFile.close();
                        return load;
                    } catch (Throwable th2) {
                        fileChannel = channel;
                        th = th2;
                        if (fileChannel != null) {
                            try {
                                fileChannel.close();
                            } catch (IOException unused) {
                            }
                        }
                        if (randomAccessFile != null) {
                            try {
                                randomAccessFile.close();
                            } catch (IOException unused2) {
                            }
                        }
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    if (fileChannel != null) {
                    }
                    if (randomAccessFile != null) {
                    }
                    throw th;
                }
            } else {
                throw new IOException("File unsuitable for memory mapping");
            }
        } catch (Throwable th4) {
            th = th4;
            randomAccessFile = null;
            if (fileChannel != null) {
            }
            if (randomAccessFile != null) {
            }
            throw th;
        }
    }

    /* renamed from: ඓ.ῲ.K.ᓣ.K$K  reason: contains not printable characters */
    /* compiled from: ByteBufferUtil */
    public static class C0844 extends InputStream {

        /* renamed from: ʬ  reason: contains not printable characters */
        public int f4068 = -1;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final ByteBuffer f4069;

        public C0844(ByteBuffer byteBuffer) {
            this.f4069 = byteBuffer;
        }

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

        public synchronized void mark(int i) {
            this.f4068 = this.f4069.position();
        }

        public boolean markSupported() {
            return true;
        }

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

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

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

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

    /* JADX WARNING: Can't wrap try/catch for region: R(7:3|4|5|6|7|8|22) */
    /* JADX WARNING: Code restructure failed: missing block: B:23:?, code lost:
        return;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x001f */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x0029 A[SYNTHETIC, Splitter:B:14:0x0029] */
    /* JADX WARNING: Removed duplicated region for block: B:18:0x0030 A[SYNTHETIC, Splitter:B:18:0x0030] */
    /* renamed from: K  reason: contains not printable characters */
    public static void m2447(ByteBuffer byteBuffer, File file) throws IOException {
        Throwable th;
        RandomAccessFile randomAccessFile;
        byteBuffer.position(0);
        FileChannel fileChannel = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            try {
                fileChannel = randomAccessFile.getChannel();
                fileChannel.write(byteBuffer);
                fileChannel.force(false);
                fileChannel.close();
                randomAccessFile.close();
                fileChannel.close();
                randomAccessFile.close();
            } catch (Throwable th2) {
                th = th2;
                if (fileChannel != null) {
                    try {
                        fileChannel.close();
                    } catch (IOException unused) {
                    }
                }
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException unused2) {
                    }
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            randomAccessFile = null;
            if (fileChannel != null) {
            }
            if (randomAccessFile != null) {
            }
            throw th;
        }
    }

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

    /* renamed from: K  reason: contains not printable characters */
    public static byte[] m2448(ByteBuffer byteBuffer) {
        C0843 r0 = (byteBuffer.isReadOnly() || !byteBuffer.hasArray()) ? null : new C0843(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
        if (r0 != null && r0.f4067 == 0 && r0.f4066 == r0.f4065.length) {
            return byteBuffer.array();
        }
        ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        byte[] bArr = new byte[asReadOnlyBuffer.limit()];
        asReadOnlyBuffer.position(0);
        asReadOnlyBuffer.get(bArr);
        return bArr;
    }
}
