package c.i.a.c.i2; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /* compiled from: AtomicFile */ public final class f { public final File a; public final File b; /* compiled from: AtomicFile */ public static final class a extends OutputStream { public final FileOutputStream i; public boolean j = false; public a(File file) throws FileNotFoundException { this.i = new FileOutputStream(file); } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { if (!this.j) { this.j = true; this.i.flush(); try { this.i.getFD().sync(); } catch (IOException e) { p.c("AtomicFile", "Failed to sync file descriptor:", e); } this.i.close(); } } @Override // java.io.OutputStream, java.io.Flushable public void flush() throws IOException { this.i.flush(); } @Override // java.io.OutputStream public void write(int i) throws IOException { this.i.write(i); } @Override // java.io.OutputStream public void write(byte[] bArr) throws IOException { this.i.write(bArr); } @Override // java.io.OutputStream public void write(byte[] bArr, int i, int i2) throws IOException { this.i.write(bArr, i, i2); } } public f(File file) { this.a = file; this.b = new File(file.getPath() + ".bak"); } public boolean a() { return this.a.exists() || this.b.exists(); } public InputStream b() throws FileNotFoundException { if (this.b.exists()) { this.a.delete(); this.b.renameTo(this.a); } return new FileInputStream(this.a); } public OutputStream c() throws IOException { if (this.a.exists()) { if (this.b.exists()) { this.a.delete(); } else if (!this.a.renameTo(this.b)) { StringBuilder K = c.d.b.a.a.K("Couldn't rename file "); K.append(this.a); K.append(" to backup file "); K.append(this.b); Log.w("AtomicFile", K.toString()); } } try { return new a(this.a); } catch (FileNotFoundException e) { File parentFile = this.a.getParentFile(); if (parentFile == null || !parentFile.mkdirs()) { StringBuilder K2 = c.d.b.a.a.K("Couldn't create "); K2.append(this.a); throw new IOException(K2.toString(), e); } try { return new a(this.a); } catch (FileNotFoundException e2) { StringBuilder K3 = c.d.b.a.a.K("Couldn't create "); K3.append(this.a); throw new IOException(K3.toString(), e2); } } } }