package com.baidu.cloud.videocache.file; import com.baidu.cloud.videocache.Cache; import com.baidu.cloud.videocache.x; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; /* loaded from: classes.dex */ public class a implements Cache { private final DiskUsage a; public File b; private RandomAccessFile c; public a(File file, DiskUsage diskUsage) { File file2; try { if (diskUsage == null) { throw new NullPointerException(); } this.a = diskUsage; b.a(file.getParentFile()); boolean exists = file.exists(); if (exists) { file2 = file; } else { file2 = new File(file.getParentFile(), file.getName() + ".download"); } this.b = file2; this.c = new RandomAccessFile(this.b, exists ? "r" : "rw"); } catch (IOException e) { throw new x("Error using file " + file + " as disc cache", e); } } private boolean a(File file) { return file.getName().endsWith(".download"); } @Override // com.baidu.cloud.videocache.Cache public synchronized int a(byte[] bArr, long j, int i) { try { this.c.seek(j); } catch (IOException e) { throw new x(String.format("Error reading %d bytes with offset %d from file[%d bytes] to buffer[%d bytes]", Integer.valueOf(i), Long.valueOf(j), Long.valueOf(available()), Integer.valueOf(bArr.length)), e); } return this.c.read(bArr, 0, i); } @Override // com.baidu.cloud.videocache.Cache public synchronized void a() { if (b()) { return; } close(); File file = new File(this.b.getParentFile(), this.b.getName().substring(0, this.b.getName().length() - 9)); if (!this.b.renameTo(file)) { throw new x("Error renaming file " + this.b + " to " + file + " for completion!"); } this.b = file; try { this.c = new RandomAccessFile(this.b, "r"); this.a.a(this.b); } catch (IOException e) { throw new x("Error opening " + this.b + " as disc cache", e); } } @Override // com.baidu.cloud.videocache.Cache public synchronized void a(byte[] bArr, int i) { try { if (b()) { throw new x("Error append cache: cache file " + this.b + " is completed!"); } this.c.seek(available()); this.c.write(bArr, 0, i); } catch (IOException e) { throw new x(String.format("Error writing %d bytes to %s from buffer with size %d", Integer.valueOf(i), this.c, Integer.valueOf(bArr.length)), e); } } @Override // com.baidu.cloud.videocache.Cache public synchronized long available() { try { } catch (IOException e) { throw new x("Error reading length of file " + this.b, e); } return (int) this.c.length(); } @Override // com.baidu.cloud.videocache.Cache public synchronized boolean b() { return !a(this.b); } @Override // com.baidu.cloud.videocache.Cache public synchronized void close() { try { this.c.close(); this.a.a(this.b); } catch (IOException e) { throw new x("Error closing file " + this.b, e); } } }