package com.bumptech.glide.util; 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; /* loaded from: classes.dex */ public final class ByteBufferUtil { static final class SafeArray { final int a; final int b; final byte[] c; SafeArray(byte[] bArr, int i, int i2) { this.c = bArr; this.a = i; this.b = i2; } } static { new AtomicReference(); } public static ByteBuffer a(File file) throws IOException { RandomAccessFile randomAccessFile; FileChannel channel; FileChannel fileChannel = 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(); } catch (Throwable th) { th = th; } try { MappedByteBuffer load = channel.map(FileChannel.MapMode.READ_ONLY, 0L, length).load(); if (channel != null) { try { channel.close(); } catch (IOException unused) { } } try { randomAccessFile.close(); } catch (IOException unused2) { } return load; } catch (Throwable th2) { fileChannel = channel; th = th2; if (fileChannel != null) { try { fileChannel.close(); } catch (IOException unused3) { } } if (randomAccessFile == null) { throw th; } try { randomAccessFile.close(); throw th; } catch (IOException unused4) { throw th; } } } catch (Throwable th3) { th = th3; randomAccessFile = null; } } public static byte[] b(ByteBuffer byteBuffer) { SafeArray a = a(byteBuffer); if (a != null && a.a == 0 && a.b == a.c.length) { return byteBuffer.array(); } ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer(); byte[] bArr = new byte[asReadOnlyBuffer.limit()]; asReadOnlyBuffer.position(0); asReadOnlyBuffer.get(bArr); return bArr; } public static InputStream c(ByteBuffer byteBuffer) { return new ByteBufferStream(byteBuffer); } private static class ByteBufferStream extends InputStream { private final ByteBuffer a; private int b = -1; ByteBufferStream(ByteBuffer byteBuffer) { this.a = byteBuffer; } @Override // java.io.InputStream public int available() { return this.a.remaining(); } @Override // java.io.InputStream public synchronized void mark(int i) { this.b = this.a.position(); } @Override // java.io.InputStream public boolean markSupported() { return true; } @Override // java.io.InputStream public int read() { if (this.a.hasRemaining()) { return this.a.get(); } return -1; } @Override // java.io.InputStream public synchronized void reset() throws IOException { if (this.b == -1) { throw new IOException("Cannot reset to unset mark position"); } this.a.position(this.b); } @Override // java.io.InputStream public long skip(long j) throws IOException { if (!this.a.hasRemaining()) { return -1L; } long min = Math.min(j, available()); this.a.position((int) (r0.position() + min)); return min; } @Override // java.io.InputStream public int read(byte[] bArr, int i, int i2) throws IOException { if (!this.a.hasRemaining()) { return -1; } int min = Math.min(i2, available()); this.a.get(bArr, i, min); return min; } } public static void a(ByteBuffer byteBuffer, File file) throws IOException { RandomAccessFile randomAccessFile; byteBuffer.position(0); FileChannel fileChannel = null; try { randomAccessFile = new RandomAccessFile(file, "rw"); } catch (Throwable th) { th = th; randomAccessFile = null; } try { fileChannel = randomAccessFile.getChannel(); fileChannel.write(byteBuffer); fileChannel.force(false); fileChannel.close(); randomAccessFile.close(); if (fileChannel != null) { try { fileChannel.close(); } catch (IOException unused) { } } try { randomAccessFile.close(); } catch (IOException unused2) { } } catch (Throwable th2) { th = th2; if (fileChannel != null) { try { fileChannel.close(); } catch (IOException unused3) { } } if (randomAccessFile != null) { try { randomAccessFile.close(); throw th; } catch (IOException unused4) { throw th; } } throw th; } } private static SafeArray a(ByteBuffer byteBuffer) { if (byteBuffer.isReadOnly() || !byteBuffer.hasArray()) { return null; } return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit()); } }