what-the-bank/sources/com/google/common/io/FileBackedOutputStream.java

209 lines
6.6 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/* loaded from: classes2.dex */
public final class FileBackedOutputStream extends OutputStream {
private File file;
private final int fileThreshold;
private MemoryOutput memory;
private OutputStream out;
private final File parentDirectory;
private final boolean resetOnFinalize;
private final ByteSource source;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class MemoryOutput extends ByteArrayOutputStream {
private MemoryOutput() {
}
byte[] getBuffer() {
return ((ByteArrayOutputStream) this).buf;
}
int getCount() {
return ((ByteArrayOutputStream) this).count;
}
}
final File getFile() {
File file;
synchronized (this) {
file = this.file;
}
return file;
}
public FileBackedOutputStream(int i) {
this(i, false);
}
public FileBackedOutputStream(int i, boolean z) {
this(i, z, null);
}
private FileBackedOutputStream(int i, boolean z, File file) {
this.fileThreshold = i;
this.resetOnFinalize = z;
this.parentDirectory = file;
MemoryOutput memoryOutput = new MemoryOutput();
this.memory = memoryOutput;
this.out = memoryOutput;
if (z) {
this.source = new ByteSource(this) { // from class: com.google.common.io.FileBackedOutputStream.1
final FileBackedOutputStream this$0;
{
this.this$0 = this;
}
@Override // com.google.common.io.ByteSource
public InputStream openStream() throws IOException {
return this.this$0.openInputStream();
}
protected void finalize() {
try {
this.this$0.reset();
} catch (Throwable th) {
th.printStackTrace(System.err);
}
}
};
} else {
this.source = new ByteSource(this) { // from class: com.google.common.io.FileBackedOutputStream.2
final FileBackedOutputStream this$0;
{
this.this$0 = this;
}
@Override // com.google.common.io.ByteSource
public InputStream openStream() throws IOException {
return this.this$0.openInputStream();
}
};
}
}
/* JADX INFO: Access modifiers changed from: private */
public InputStream openInputStream() throws IOException {
synchronized (this) {
if (this.file != null) {
return new FileInputStream(this.file);
}
return new ByteArrayInputStream(this.memory.getBuffer(), 0, this.memory.getCount());
}
}
public final void reset() throws IOException {
synchronized (this) {
try {
close();
MemoryOutput memoryOutput = this.memory;
if (memoryOutput == null) {
this.memory = new MemoryOutput();
} else {
memoryOutput.reset();
}
this.out = this.memory;
File file = this.file;
if (file != null) {
this.file = null;
if (!file.delete()) {
String valueOf = String.valueOf(file);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 18);
sb.append("Could not delete: ");
sb.append(valueOf);
throw new IOException(sb.toString());
}
}
} catch (Throwable th) {
if (this.memory == null) {
this.memory = new MemoryOutput();
} else {
this.memory.reset();
}
this.out = this.memory;
File file2 = this.file;
if (file2 != null) {
this.file = null;
if (!file2.delete()) {
String valueOf2 = String.valueOf(file2);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 18);
sb2.append("Could not delete: ");
sb2.append(valueOf2);
throw new IOException(sb2.toString());
}
}
throw th;
}
}
}
@Override // java.io.OutputStream
public final void write(int i) throws IOException {
synchronized (this) {
update(1);
this.out.write(i);
}
}
@Override // java.io.OutputStream
public final void write(byte[] bArr) throws IOException {
synchronized (this) {
write(bArr, 0, bArr.length);
}
}
@Override // java.io.OutputStream
public final void write(byte[] bArr, int i, int i2) throws IOException {
synchronized (this) {
update(i2);
this.out.write(bArr, i, i2);
}
}
@Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
public final void close() throws IOException {
synchronized (this) {
this.out.close();
}
}
@Override // java.io.OutputStream, java.io.Flushable
public final void flush() throws IOException {
synchronized (this) {
this.out.flush();
}
}
private void update(int i) throws IOException {
if (this.file != null || this.memory.getCount() + i <= this.fileThreshold) {
return;
}
File createTempFile = File.createTempFile("FileBackedOutputStream", null, this.parentDirectory);
if (this.resetOnFinalize) {
createTempFile.deleteOnExit();
}
FileOutputStream fileOutputStream = new FileOutputStream(createTempFile);
fileOutputStream.write(this.memory.getBuffer(), 0, this.memory.getCount());
fileOutputStream.flush();
this.out = fileOutputStream;
this.file = createTempFile;
this.memory = null;
}
public final ByteSource asByteSource() {
return this.source;
}
}