Rabbit-R1/switch port/java/sources/com/google/common/io/Closer.java

144 lines
4.7 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package com.google.common.io;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
@ElementTypesAreNonnullByDefault
/* loaded from: classes3.dex */
public final class Closer implements Closeable {
private static final Suppressor SUPPRESSOR;
private final Deque<Closeable> stack = new ArrayDeque(4);
final Suppressor suppressor;
@CheckForNull
private Throwable thrown;
/* loaded from: classes3.dex */
interface Suppressor {
void suppress(Closeable closeable, Throwable th, Throwable th2);
}
static {
Suppressor tryCreate = SuppressingSuppressor.tryCreate();
if (tryCreate == null) {
tryCreate = LoggingSuppressor.INSTANCE;
}
SUPPRESSOR = tryCreate;
}
public static Closer create() {
return new Closer(SUPPRESSOR);
}
Closer(Suppressor suppressor) {
this.suppressor = (Suppressor) Preconditions.checkNotNull(suppressor);
}
@ParametricNullness
public <C extends Closeable> C register(@ParametricNullness C c) {
if (c != null) {
this.stack.addFirst(c);
}
return c;
}
public RuntimeException rethrow(Throwable th) throws IOException {
Preconditions.checkNotNull(th);
this.thrown = th;
Throwables.propagateIfPossible(th, IOException.class);
throw new RuntimeException(th);
}
public <X extends Exception> RuntimeException rethrow(Throwable th, Class<X> cls) throws IOException, Exception {
Preconditions.checkNotNull(th);
this.thrown = th;
Throwables.propagateIfPossible(th, IOException.class);
Throwables.propagateIfPossible(th, cls);
throw new RuntimeException(th);
}
public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(Throwable th, Class<X1> cls, Class<X2> cls2) throws IOException, Exception, Exception {
Preconditions.checkNotNull(th);
this.thrown = th;
Throwables.propagateIfPossible(th, IOException.class);
Throwables.propagateIfPossible(th, cls, cls2);
throw new RuntimeException(th);
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
Throwable th = this.thrown;
while (!this.stack.isEmpty()) {
Closeable removeFirst = this.stack.removeFirst();
try {
removeFirst.close();
} catch (Throwable th2) {
if (th == null) {
th = th2;
} else {
this.suppressor.suppress(removeFirst, th, th2);
}
}
}
if (this.thrown != null || th == null) {
return;
}
Throwables.propagateIfPossible(th, IOException.class);
throw new AssertionError(th);
}
/* loaded from: classes3.dex */
static final class LoggingSuppressor implements Suppressor {
static final LoggingSuppressor INSTANCE = new LoggingSuppressor();
LoggingSuppressor() {
}
@Override // com.google.common.io.Closer.Suppressor
public void suppress(Closeable closeable, Throwable th, Throwable th2) {
Logger logger = Closeables.logger;
Level level = Level.WARNING;
String valueOf = String.valueOf(closeable);
logger.log(level, new StringBuilder(String.valueOf(valueOf).length() + 42).append("Suppressing exception thrown when closing ").append(valueOf).toString(), th2);
}
}
/* loaded from: classes3.dex */
static final class SuppressingSuppressor implements Suppressor {
private final Method addSuppressed;
@CheckForNull
static SuppressingSuppressor tryCreate() {
try {
return new SuppressingSuppressor(Throwable.class.getMethod("addSuppressed", Throwable.class));
} catch (Throwable unused) {
return null;
}
}
private SuppressingSuppressor(Method method) {
this.addSuppressed = method;
}
@Override // com.google.common.io.Closer.Suppressor
public void suppress(Closeable closeable, Throwable th, Throwable th2) {
if (th == th2) {
return;
}
try {
this.addSuppressed.invoke(th, th2);
} catch (Throwable unused) {
LoggingSuppressor.INSTANCE.suppress(closeable, th, th2);
}
}
}
}