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; /* loaded from: classes2.dex */ public final class Closer implements Closeable { private static final Suppressor SUPPRESSOR; private final Deque stack = new ArrayDeque(4); final Suppressor suppressor; private Throwable thrown; /* loaded from: classes2.dex */ interface Suppressor { void suppress(Closeable closeable, Throwable th, Throwable th2); } static { Suppressor suppressor; if (SuppressingSuppressor.isAvailable()) { suppressor = SuppressingSuppressor.INSTANCE; } else { suppressor = LoggingSuppressor.INSTANCE; } SUPPRESSOR = suppressor; } public static Closer create() { return new Closer(SUPPRESSOR); } Closer(Suppressor suppressor) { this.suppressor = (Suppressor) Preconditions.checkNotNull(suppressor); } public final C register(C c) { if (c != null) { this.stack.addFirst(c); } return c; } public final RuntimeException rethrow(Throwable th) throws IOException { Preconditions.checkNotNull(th); this.thrown = th; Throwables.propagateIfPossible(th, IOException.class); throw new RuntimeException(th); } public final RuntimeException rethrow(Throwable th, Class cls) throws IOException, Exception { Preconditions.checkNotNull(th); this.thrown = th; Throwables.propagateIfPossible(th, IOException.class); Throwables.propagateIfPossible(th, cls); throw new RuntimeException(th); } public final RuntimeException rethrow(Throwable th, Class cls, Class 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 final 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: classes2.dex */ static final class LoggingSuppressor implements Suppressor { static final LoggingSuppressor INSTANCE = new LoggingSuppressor(); LoggingSuppressor() { } @Override // com.google.common.io.Closer.Suppressor public final void suppress(Closeable closeable, Throwable th, Throwable th2) { Logger logger = Closeables.logger; Level level = Level.WARNING; String valueOf = String.valueOf(closeable); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 42); sb.append("Suppressing exception thrown when closing "); sb.append(valueOf); logger.log(level, sb.toString(), th2); } } /* loaded from: classes2.dex */ static final class SuppressingSuppressor implements Suppressor { static final SuppressingSuppressor INSTANCE = new SuppressingSuppressor(); static final Method addSuppressed = addSuppressedMethodOrNull(); SuppressingSuppressor() { } private static Method addSuppressedMethodOrNull() { try { return Throwable.class.getMethod("addSuppressed", Throwable.class); } catch (Throwable unused) { return null; } } @Override // com.google.common.io.Closer.Suppressor public final void suppress(Closeable closeable, Throwable th, Throwable th2) { if (th == th2) { return; } try { addSuppressed.invoke(th, th2); } catch (Throwable unused) { LoggingSuppressor.INSTANCE.suppress(closeable, th, th2); } } static boolean isAvailable() { return addSuppressed != null; } } }