package o; import com.airbnb.deeplinkdispatch.base.MatchIndex; import com.google.common.primitives.UnsignedBytes; 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; /* loaded from: classes.dex */ public final class Qeb { private static final AtomicReference d = new AtomicReference<>(); public static ByteBuffer b(File file) throws IOException { RandomAccessFile randomAccessFile; Throwable th; FileChannel fileChannel; FileChannel fileChannel2 = 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, MatchIndex.ROOT_VALUE); try { fileChannel = randomAccessFile.getChannel(); try { MappedByteBuffer load = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0L, length).load(); if (fileChannel != null) { try { fileChannel.close(); } catch (IOException unused) { } } try { randomAccessFile.close(); } catch (IOException unused2) { } return load; } catch (Throwable th2) { th = th2; Throwable th3 = th; fileChannel2 = fileChannel; th = th3; if (fileChannel2 != null) { try { fileChannel2.close(); } catch (IOException unused3) { } } if (randomAccessFile != null) { try { randomAccessFile.close(); throw th; } catch (IOException unused4) { throw th; } } throw th; } } catch (Throwable th4) { th = th4; fileChannel = null; } } catch (Throwable th5) { th = th5; randomAccessFile = null; } } public static InputStream a(ByteBuffer byteBuffer) { return new IeS(byteBuffer); } public static ByteBuffer a(InputStream inputStream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(16384); byte[] andSet = d.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 { d.set(andSet); byte[] byteArray = byteArrayOutputStream.toByteArray(); return (ByteBuffer) ByteBuffer.allocateDirect(byteArray.length).put(byteArray).position(0); } } } public static ByteBuffer e(ByteBuffer byteBuffer) { return (ByteBuffer) byteBuffer.position(0); } /* loaded from: classes.dex */ static final class LWm { final int a; final byte[] b; final int e; LWm(byte[] bArr, int i, int i2) { this.b = bArr; this.e = i; this.a = i2; } } /* loaded from: classes.dex */ static class IeS extends InputStream { private final ByteBuffer c; private int d = -1; @Override // java.io.InputStream public final boolean markSupported() { return true; } IeS(ByteBuffer byteBuffer) { this.c = byteBuffer; } @Override // java.io.InputStream public final int available() { return this.c.remaining(); } @Override // java.io.InputStream public final int read() { if (this.c.hasRemaining()) { return this.c.get() & UnsignedBytes.MAX_VALUE; } return -1; } @Override // java.io.InputStream public final void mark(int i) { synchronized (this) { this.d = this.c.position(); } } @Override // java.io.InputStream public final int read(byte[] bArr, int i, int i2) { if (!this.c.hasRemaining()) { return -1; } int min = Math.min(i2, available()); this.c.get(bArr, i, min); return min; } @Override // java.io.InputStream public final void reset() throws IOException { synchronized (this) { int i = this.d; if (i == -1) { throw new IOException("Cannot reset to unset mark position"); } this.c.position(i); } } @Override // java.io.InputStream public final long skip(long j) { if (!this.c.hasRemaining()) { return -1L; } long min = Math.min(j, available()); this.c.position((int) (r0.position() + min)); return min; } } public static byte[] d(ByteBuffer byteBuffer) { LWm lWm = (byteBuffer.isReadOnly() || !byteBuffer.hasArray()) ? null : new LWm(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit()); if (lWm != null && lWm.e == 0 && lWm.a == lWm.b.length) { return byteBuffer.array(); } ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer(); byte[] bArr = new byte[asReadOnlyBuffer.limit()]; asReadOnlyBuffer.get(bArr); return bArr; } public static void c(ByteBuffer byteBuffer, File file) throws IOException { RandomAccessFile randomAccessFile; FileChannel fileChannel = null; try { randomAccessFile = new RandomAccessFile(file, "rw"); try { FileChannel channel = randomAccessFile.getChannel(); try { channel.write(byteBuffer); channel.force(false); channel.close(); randomAccessFile.close(); if (channel != null) { try { channel.close(); } catch (IOException unused) { } } try { randomAccessFile.close(); } catch (IOException unused2) { } } catch (Throwable th) { th = th; fileChannel = channel; if (fileChannel != null) { try { fileChannel.close(); } catch (IOException unused3) { } } if (randomAccessFile != null) { try { randomAccessFile.close(); throw th; } catch (IOException unused4) { throw th; } } throw th; } } catch (Throwable th2) { th = th2; } } catch (Throwable th3) { th = th3; randomAccessFile = null; } } }