package b7; 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: classes2.dex */ public abstract class a { /* renamed from: a, reason: collision with root package name */ private static final AtomicReference f3304a = new AtomicReference(); /* renamed from: b7.a$a, reason: collision with other inner class name */ private static class C0055a extends InputStream { /* renamed from: a, reason: collision with root package name */ private final ByteBuffer f3305a; /* renamed from: b, reason: collision with root package name */ private int f3306b = -1; C0055a(ByteBuffer byteBuffer) { this.f3305a = byteBuffer; } @Override // java.io.InputStream public int available() { return this.f3305a.remaining(); } @Override // java.io.InputStream public synchronized void mark(int i10) { this.f3306b = this.f3305a.position(); } @Override // java.io.InputStream public boolean markSupported() { return true; } @Override // java.io.InputStream public int read() { if (this.f3305a.hasRemaining()) { return this.f3305a.get() & 255; } return -1; } @Override // java.io.InputStream public synchronized void reset() { int i10 = this.f3306b; if (i10 == -1) { throw new IOException("Cannot reset to unset mark position"); } this.f3305a.position(i10); } @Override // java.io.InputStream public long skip(long j10) { if (!this.f3305a.hasRemaining()) { return -1L; } long min = Math.min(j10, available()); this.f3305a.position((int) (r0.position() + min)); return min; } @Override // java.io.InputStream public int read(byte[] bArr, int i10, int i11) { if (!this.f3305a.hasRemaining()) { return -1; } int min = Math.min(i11, available()); this.f3305a.get(bArr, i10, min); return min; } } static final class b { /* renamed from: a, reason: collision with root package name */ final int f3307a; /* renamed from: b, reason: collision with root package name */ final int f3308b; /* renamed from: c, reason: collision with root package name */ final byte[] f3309c; b(byte[] bArr, int i10, int i11) { this.f3309c = bArr; this.f3307a = i10; this.f3308b = i11; } } private static b a(ByteBuffer byteBuffer) { if (byteBuffer.isReadOnly() || !byteBuffer.hasArray()) { return null; } return new b(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit()); } public static ByteBuffer b(File file) { 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, "r"); try { fileChannel = randomAccessFile.getChannel(); } catch (Throwable th2) { th = th2; fileChannel = null; } try { MappedByteBuffer load = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0L, length).load(); try { fileChannel.close(); } catch (IOException unused) { } try { randomAccessFile.close(); } catch (IOException unused2) { } return load; } catch (Throwable th3) { th = th3; Throwable th4 = th; fileChannel2 = fileChannel; th = th4; if (fileChannel2 != null) { try { fileChannel2.close(); } catch (IOException unused3) { } } if (randomAccessFile == null) { throw th; } try { randomAccessFile.close(); throw th; } catch (IOException unused4) { throw th; } } } catch (Throwable th5) { th = th5; randomAccessFile = null; } } public static ByteBuffer c(InputStream inputStream) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(16384); byte[] bArr = (byte[]) f3304a.getAndSet(null); if (bArr == null) { bArr = new byte[16384]; } while (true) { int read = inputStream.read(bArr); if (read < 0) { f3304a.set(bArr); byte[] byteArray = byteArrayOutputStream.toByteArray(); return d(ByteBuffer.allocateDirect(byteArray.length).put(byteArray)); } byteArrayOutputStream.write(bArr, 0, read); } } public static ByteBuffer d(ByteBuffer byteBuffer) { return (ByteBuffer) byteBuffer.position(0); } public static byte[] e(ByteBuffer byteBuffer) { b a10 = a(byteBuffer); if (a10 != null && a10.f3307a == 0 && a10.f3308b == a10.f3309c.length) { return byteBuffer.array(); } ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer(); byte[] bArr = new byte[asReadOnlyBuffer.limit()]; d(asReadOnlyBuffer); asReadOnlyBuffer.get(bArr); return bArr; } public static void f(ByteBuffer byteBuffer, File file) { RandomAccessFile randomAccessFile; FileChannel channel; d(byteBuffer); FileChannel fileChannel = null; try { randomAccessFile = new RandomAccessFile(file, "rw"); try { channel = randomAccessFile.getChannel(); } catch (Throwable th) { th = th; } } catch (Throwable th2) { th = th2; randomAccessFile = null; } try { channel.write(byteBuffer); channel.force(false); channel.close(); randomAccessFile.close(); try { channel.close(); } catch (IOException unused) { } try { randomAccessFile.close(); } catch (IOException unused2) { } } catch (Throwable th3) { th = th3; fileChannel = channel; if (fileChannel != null) { try { fileChannel.close(); } catch (IOException unused3) { } } if (randomAccessFile == null) { throw th; } try { randomAccessFile.close(); throw th; } catch (IOException unused4) { throw th; } } } public static InputStream g(ByteBuffer byteBuffer) { return new C0055a(byteBuffer); } }