Closer.java 4.69 KB
package com.google.common.io;

import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
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.Nullable;

@Beta
public final class Closer
  implements Closeable
{
  private static final c b;
  @VisibleForTesting
  final c a;
  private final Deque<Closeable> c = new ArrayDeque(4);
  private Throwable d;
  
  static
  {
    if (b.a()) {}
    for (Object localObject = b.a;; localObject = a.a)
    {
      b = (c)localObject;
      return;
    }
  }
  
  @VisibleForTesting
  private Closer(c paramc)
  {
    this.a = ((c)Preconditions.checkNotNull(paramc));
  }
  
  public static Closer create()
  {
    return new Closer(b);
  }
  
  public final void close()
    throws IOException
  {
    Object localObject = this.d;
    while (!this.c.isEmpty())
    {
      Closeable localCloseable = (Closeable)this.c.removeFirst();
      try
      {
        localCloseable.close();
      }
      catch (Throwable localThrowable)
      {
        if (localObject == null) {
          localObject = localThrowable;
        } else {
          this.a.a(localCloseable, (Throwable)localObject, localThrowable);
        }
      }
    }
    if ((this.d == null) && (localObject != null))
    {
      Throwables.propagateIfPossible((Throwable)localObject, IOException.class);
      throw new AssertionError(localObject);
    }
  }
  
  public final <C extends Closeable> C register(@Nullable C paramC)
  {
    if (paramC != null) {
      this.c.addFirst(paramC);
    }
    return paramC;
  }
  
  public final RuntimeException rethrow(Throwable paramThrowable)
    throws IOException
  {
    Preconditions.checkNotNull(paramThrowable);
    this.d = paramThrowable;
    Throwables.propagateIfPossible(paramThrowable, IOException.class);
    throw new RuntimeException(paramThrowable);
  }
  
  public final <X extends Exception> RuntimeException rethrow(Throwable paramThrowable, Class<X> paramClass)
    throws IOException, Exception
  {
    Preconditions.checkNotNull(paramThrowable);
    this.d = paramThrowable;
    Throwables.propagateIfPossible(paramThrowable, IOException.class);
    Throwables.propagateIfPossible(paramThrowable, paramClass);
    throw new RuntimeException(paramThrowable);
  }
  
  public final <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(Throwable paramThrowable, Class<X1> paramClass, Class<X2> paramClass1)
    throws IOException, Exception, Exception
  {
    Preconditions.checkNotNull(paramThrowable);
    this.d = paramThrowable;
    Throwables.propagateIfPossible(paramThrowable, IOException.class);
    Throwables.propagateIfPossible(paramThrowable, paramClass, paramClass1);
    throw new RuntimeException(paramThrowable);
  }
  
  @VisibleForTesting
  static final class a
    implements Closer.c
  {
    static final a a = new a();
    
    public final void a(Closeable paramCloseable, Throwable paramThrowable1, Throwable paramThrowable2)
    {
      paramThrowable1 = Closeables.a;
      Level localLevel = Level.WARNING;
      paramCloseable = String.valueOf(String.valueOf(paramCloseable));
      paramThrowable1.log(localLevel, paramCloseable.length() + 42 + "Suppressing exception thrown when closing " + paramCloseable, paramThrowable2);
    }
  }
  
  @VisibleForTesting
  static final class b
    implements Closer.c
  {
    static final b a = new b();
    static final Method b = b();
    
    static boolean a()
    {
      return b != null;
    }
    
    private static Method b()
    {
      try
      {
        Method localMethod = Throwable.class.getMethod("addSuppressed", new Class[] { Throwable.class });
        return localMethod;
      }
      catch (Throwable localThrowable) {}
      return null;
    }
    
    public final void a(Closeable paramCloseable, Throwable paramThrowable1, Throwable paramThrowable2)
    {
      if (paramThrowable1 == paramThrowable2) {
        return;
      }
      try
      {
        b.invoke(paramThrowable1, new Object[] { paramThrowable2 });
        return;
      }
      catch (Throwable localThrowable)
      {
        Closer.a.a.a(paramCloseable, paramThrowable1, paramThrowable2);
      }
    }
  }
  
  @VisibleForTesting
  static abstract interface c
  {
    public abstract void a(Closeable paramCloseable, Throwable paramThrowable1, Throwable paramThrowable2);
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/google/common/io/Closer.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */