FileBackedOutputStream.java 5.91 KB
package com.google.common.io;

import com.google.common.annotations.Beta;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Beta
public final class FileBackedOutputStream
  extends OutputStream
{
  private final int a;
  private final boolean b;
  private final ByteSource c;
  private OutputStream d;
  private a e;
  private File f;
  
  public FileBackedOutputStream(int paramInt)
  {
    this(paramInt, false);
  }
  
  public FileBackedOutputStream(int paramInt, boolean paramBoolean)
  {
    this.a = paramInt;
    this.b = paramBoolean;
    this.e = new a((byte)0);
    this.d = this.e;
    if (paramBoolean)
    {
      this.c = new ByteSource()
      {
        protected final void finalize()
        {
          try
          {
            FileBackedOutputStream.this.reset();
            return;
          }
          catch (Throwable localThrowable)
          {
            localThrowable.printStackTrace(System.err);
          }
        }
        
        public final InputStream openStream()
          throws IOException
        {
          return FileBackedOutputStream.a(FileBackedOutputStream.this);
        }
      };
      return;
    }
    this.c = new ByteSource()
    {
      public final InputStream openStream()
        throws IOException
      {
        return FileBackedOutputStream.a(FileBackedOutputStream.this);
      }
    };
  }
  
  /* Error */
  private InputStream a()
    throws IOException
  {
    // Byte code:
    //   0: aload_0
    //   1: monitorenter
    //   2: aload_0
    //   3: getfield 54	com/google/common/io/FileBackedOutputStream:f	Ljava/io/File;
    //   6: ifnull +19 -> 25
    //   9: new 56	java/io/FileInputStream
    //   12: dup
    //   13: aload_0
    //   14: getfield 54	com/google/common/io/FileBackedOutputStream:f	Ljava/io/File;
    //   17: invokespecial 59	java/io/FileInputStream:<init>	(Ljava/io/File;)V
    //   20: astore_1
    //   21: aload_0
    //   22: monitorexit
    //   23: aload_1
    //   24: areturn
    //   25: new 61	java/io/ByteArrayInputStream
    //   28: dup
    //   29: aload_0
    //   30: getfield 41	com/google/common/io/FileBackedOutputStream:e	Lcom/google/common/io/FileBackedOutputStream$a;
    //   33: invokevirtual 64	com/google/common/io/FileBackedOutputStream$a:a	()[B
    //   36: iconst_0
    //   37: aload_0
    //   38: getfield 41	com/google/common/io/FileBackedOutputStream:e	Lcom/google/common/io/FileBackedOutputStream$a;
    //   41: invokevirtual 67	com/google/common/io/FileBackedOutputStream$a:b	()I
    //   44: invokespecial 70	java/io/ByteArrayInputStream:<init>	([BII)V
    //   47: astore_1
    //   48: goto -27 -> 21
    //   51: astore_1
    //   52: aload_0
    //   53: monitorexit
    //   54: aload_1
    //   55: athrow
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	56	0	this	FileBackedOutputStream
    //   20	28	1	localObject1	Object
    //   51	4	1	localObject2	Object
    // Exception table:
    //   from	to	target	type
    //   2	21	51	finally
    //   25	48	51	finally
  }
  
  private void a(int paramInt)
    throws IOException
  {
    if ((this.f == null) && (this.e.b() + paramInt > this.a))
    {
      File localFile = File.createTempFile("FileBackedOutputStream", null);
      if (this.b) {
        localFile.deleteOnExit();
      }
      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
      localFileOutputStream.write(this.e.a(), 0, this.e.b());
      localFileOutputStream.flush();
      this.d = localFileOutputStream;
      this.f = localFile;
      this.e = null;
    }
  }
  
  public final ByteSource asByteSource()
  {
    return this.c;
  }
  
  public final void close()
    throws IOException
  {
    try
    {
      this.d.close();
      return;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public final void flush()
    throws IOException
  {
    try
    {
      this.d.flush();
      return;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public final void reset()
    throws IOException
  {
    try
    {
      Object localObject1;
      File localFile;
      String str;
      return;
    }
    finally
    {
      if (this.e == null) {
        this.e = new a((byte)0);
      }
      for (;;)
      {
        this.d = this.e;
        if (this.f == null) {
          break;
        }
        localFile = this.f;
        this.f = null;
        if (localFile.delete()) {
          break;
        }
        str = String.valueOf(String.valueOf(localFile));
        throw new IOException(str.length() + 18 + "Could not delete: " + str);
        this.e.reset();
      }
    }
  }
  
  public final void write(int paramInt)
    throws IOException
  {
    try
    {
      a(1);
      this.d.write(paramInt);
      return;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public final void write(byte[] paramArrayOfByte)
    throws IOException
  {
    try
    {
      write(paramArrayOfByte, 0, paramArrayOfByte.length);
      return;
    }
    finally
    {
      paramArrayOfByte = finally;
      throw paramArrayOfByte;
    }
  }
  
  public final void write(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    try
    {
      a(paramInt2);
      this.d.write(paramArrayOfByte, paramInt1, paramInt2);
      return;
    }
    finally
    {
      paramArrayOfByte = finally;
      throw paramArrayOfByte;
    }
  }
  
  static final class a
    extends ByteArrayOutputStream
  {
    final byte[] a()
    {
      return this.buf;
    }
    
    final int b()
    {
      return this.count;
    }
  }
}


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