package com.worklight.location.internal.events.nativeImpl;

import com.worklight.location.internal.events.storage.ChunkStorage;
import com.worklight.location.internal.events.storage.ChunkStringFactory;
import com.worklight.location.internal.events.storage.IChunkStorage;
import com.worklight.location.internal.events.storage.IPersistentStorageManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.OverlappingFileLockException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.UUID;

public class FilePersistentStorageManager
  implements IPersistentStorageManager
{
  private final Collection<FileLockResources> lockResources = new ArrayList();
  private final long maximumMemorySize;
  private final File workingDir;

  public FilePersistentStorageManager(File paramFile, long paramLong)
  {
    if ((!paramFile.exists()) || (!paramFile.isDirectory()))
      throw new IllegalArgumentException("workingDir is not an existing directory");
    this.workingDir = paramFile;
    if (paramLong <= 0L)
      throw new IllegalArgumentException("maximumMemorySize must be a positive integer, was " + paramLong);
    this.maximumMemorySize = paramLong;
  }

  private static void deleteDirectory(File paramFile)
  {
    for (File localFile : paramFile.listFiles())
      if (!localFile.delete())
        throw new RuntimeException("Could not delete file " + localFile.getAbsolutePath());
    if (!paramFile.delete())
      throw new RuntimeException("Could not delete dir " + paramFile.getName());
  }

  private static FileLockResources tryLockDirectory(File paramFile)
  {
    try
    {
      File localFile = new File(paramFile, ".filelock");
      if (!localFile.exists())
        localFile.createNewFile();
      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
      FileChannel localFileChannel = localFileOutputStream.getChannel();
      FileLockResources localFileLockResources = new FileLockResources(localFileOutputStream, localFileChannel, localFileChannel.lock());
      return localFileLockResources;
    }
    catch (IOException localIOException)
    {
      throw new AssertionError(localIOException);
    }
    catch (OverlappingFileLockException localOverlappingFileLockException)
    {
    }
    return null;
  }

  void clearAll()
    throws Exception
  {
    Iterator localIterator = this.lockResources.iterator();
    while (localIterator.hasNext())
      ((FileLockResources)localIterator.next()).close();
    this.lockResources.clear();
    System.gc();
    Thread.sleep(10L);
    File[] arrayOfFile = this.workingDir.listFiles();
    int i = arrayOfFile.length;
    for (int j = 0; j < i; j++)
      deleteDirectory(arrayOfFile[j]);
  }

  public void clearOldPersistentData()
  {
    for (File localFile : this.workingDir.listFiles())
    {
      FileLockResources localFileLockResources = tryLockDirectory(localFile);
      if (localFileLockResources != null)
      {
        localFileLockResources.close();
        deleteDirectory(localFile);
      }
    }
  }

  public IChunkStorage createStorage()
  {
    File localFile = new File(this.workingDir, UUID.randomUUID().toString());
    localFile.mkdir();
    FileLockResources localFileLockResources = tryLockDirectory(localFile);
    if (localFileLockResources == null)
      throw new AssertionError("Could not lock the file");
    this.lockResources.add(localFileLockResources);
    return new ChunkStorage(this.maximumMemorySize, new FileChunkStorage(localFile, new ChunkStringFactory()));
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.location.internal.events.nativeImpl.FilePersistentStorageManager
 * JD-Core Version:    0.6.2
 */