// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:43:11
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/app/FileCache

package com.macromedia.generator.app;

import com.macromedia.generator.utils.GenUtils;
import java.io.*;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.*;

// Referenced classes of package com.macromedia.generator.app:
//            URLCache, GenCache, GenCacheEntry, CacheLog

public class FileCache extends URLCache
{

    public FileCache(long l, boolean flag, boolean flag1, long l1, boolean flag2, 
            boolean flag3, int i, int j, CacheLog cachelog)
    {
        super(l, flag, flag1, l1, flag2, i, j, cachelog);
        D3 = null;
        D4 = false;
        D5 = false;
        D5 = flag3;
        if(D5)
            loadIndex();
        else
        if(l > 0L)
            D0(true);
    }

    private synchronized void D0(boolean flag)
    {
        if(mIndex == null)
            return;
        logCacheMessage(getResString("CleaningCacheDirectory"), 2);
        File file = D3();
        String s = null;
        if(file != null && file.exists())
            s = file.getPath();
        String s1 = D4();
        if(s1 == null)
            return;
        File file1 = new File(makeJavaPretty(s1));
        if(file1 == null)
            return;
        File afile[] = null;
        if(GenUtils.isJava2())
        {
            afile = file1.listFiles();
            if(afile == null)
                return;
        } else
        {
            String as[] = file1.list();
            if(as == null)
                return;
            afile = GenUtils.makeFileArray(file1, as);
            if(afile == null)
                return;
        }
        for(int i = 0; i < Array.getLength(afile); i++)
            if(afile[i] != null)
            {
                String s2 = afile[i].getPath();
                if(flag || s == null || !s2.equals(s))
                {
                    boolean flag1 = true;
                    GenCacheEntry gencacheentry = null;
                    Enumeration enumeration = mIndex.elements();
                    if(enumeration == null)
                        return;
                    while(enumeration.hasMoreElements()) 
                    {
                        gencacheentry = (GenCacheEntry)enumeration.nextElement();
                        if(gencacheentry.value.equals(s2))
                        {
                            if(!isCacheEntryExpired(gencacheentry))
                                flag1 = false;
                            break;
                        }
                    }
                    if(flag1)
                        if(gencacheentry != null)
                        {
                            removeCacheEntry(gencacheentry);
                        } else
                        {
                            logCacheMessage(getResString("DeletingCacheFile") + afile[i].toString(), 2);
                            afile[i].delete();
                        }
                }
            }

    }

    private File D1()
    {
        File file = null;
        try
        {
            file = D3();
            if(file == null)
                throw new IOException(getResString("CouldntGetIndexFile"));
            if(!file.exists())
            {
                if(!GenUtils.createNewFile(file))
                    throw new IOException(getResString("CreateNewFileFailed"));
            } else
            {
                file.delete();
                if(!GenUtils.createNewFile(file))
                    throw new IOException(getResString("CreateNewFileFailed"));
            }
        }
        catch(IOException ioexception)
        {
            System.out.println(ioexception);
            file = null;
        }
        catch(SecurityException securityexception)
        {
            System.out.println(securityexception);
            file = null;
        }
        return file;
    }

    private File D2(String s)
    {
        File file = null;
        try
        {
            if(s.length() != 4)
                s = ".gen";
            String s1 = D4();
            if(s1 == null)
                throw new IOException(getResString("InvalidCachePath"));
            s1 = makeJavaPretty(s1);
            File file1 = new File(s1);
            if(file1 == null)
                throw new IOException(getResString("InvalidCacheDirectory"));
            file = GenUtils.createTempFile("gen", s, file1);
            logCacheMessage(getResString("CreatedNewCacheFile") + file.getName(), 5);
        }
        catch(IOException ioexception)
        {
            System.out.println(ioexception);
            file = null;
        }
        return file;
    }

    protected void dispose()
    {
        if(D4)
        {
            return;
        } else
        {
            flush();
            return;
        }
    }

    public void flush()
    {
        if(!isCacheEnabled())
        {
            return;
        } else
        {
            logCacheMessage(getResString("Flushing"), 2);
            D9();
            D4 = true;
            return;
        }
    }

    private File D3()
    {
        String s = D4();
        if(s == null)
        {
            return null;
        } else
        {
            s = s + "/index.dat";
            File file = new File(makeJavaPretty(s));
            return file;
        }
    }

    private String D4()
    {
        if(D3 == null)
        {
            String s = System.getProperty("com.macromedia.generator.installpath");
            String s1 = System.getProperty("file.separator");
            s = s.replace(s1.charAt(0), "/".charAt(0));
            D3 = s + "/" + "cache" + "/";
            File file = new File(makeJavaPretty(D3));
            if(!file.exists() && !file.mkdir())
                return null;
        }
        return D3;
    }

    protected Object getCacheURL(URL url)
    {
        byte abyte0[] = null;
        String s = (String)super.getCacheURL(url);
        if(s == null)
            return null;
        try
        {
            abyte0 = readFile(s);
        }
        catch(IOException _ex)
        {
            return null;
        }
        return abyte0;
    }

    public boolean getLoad()
    {
        return D5;
    }

    public void loadIndex()
    {
        logCacheMessage(getResString("LoadingIndex"), 2);
        if(isCacheEnabled())
            D6();
    }

    protected void logCacheMessage(String s, int i)
    {
        if(mLog != null)
        {
            String s1 = "FileCache: " + s;
            super.logCacheMessage(s1, i);
        }
    }

    private File D5(String s)
    {
        if(s == null)
            return null;
        File file = new File(makeJavaPretty(s));
        if(file.exists())
            return file;
        else
            return null;
    }

    private synchronized void D6()
    {
        if(mIndex == null)
            return;
        File file = D3();
        if(file == null || !file.exists())
            return;
        String s = D4();
        if(s == null)
            return;
        File file1 = new File(makeJavaPretty(s));
        if(file1 == null)
            return;
        try
        {
            FileInputStream fileinputstream = new FileInputStream(file);
            BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(fileinputstream));
            try
            {
                String s1 = bufferedreader.readLine();
                if(!s1.equals("Generator Disk Cache Index - DO NOT EDIT!"))
                    return;
                for(String s2 = bufferedreader.readLine(); s2 != null; s2 = bufferedreader.readLine())
                {
                    StringTokenizer stringtokenizer = new StringTokenizer(s2, ",");
                    GenCacheEntry gencacheentry = new GenCacheEntry();
                    gencacheentry.key = stringtokenizer.nextToken();
                    gencacheentry.value = stringtokenizer.nextToken();
                    gencacheentry.size = Long.parseLong(stringtokenizer.nextToken());
                    gencacheentry.time = Long.parseLong(stringtokenizer.nextToken());
                    gencacheentry.expires = Long.parseLong(stringtokenizer.nextToken());
                    gencacheentry.hits = 0L;
                    if(!isCacheEntryExpired(gencacheentry) && D8((String)gencacheentry.value) && !setCacheEntry(gencacheentry))
                        break;
                }

                bufferedreader.close();
                D0(false);
            }
            catch(IOException ioexception)
            {
                System.out.println(ioexception);
            }
        }
        catch(FileNotFoundException filenotfoundexception)
        {
            System.out.println(filenotfoundexception);
        }
    }

    protected byte[] readURLInputStream(URL url, InputStream inputstream, long l, int i, boolean flag, long l1)
        throws IOException
    {
        byte abyte0[] = null;
        try
        {
            if(isCacheEnabled() && (getForce() || flag))
            {
                String s = url.getFile();
                int j = s.lastIndexOf(46);
                if(j != -1)
                    s = s.substring(j);
                j = s.indexOf(63);
                if(j != -1)
                    s = s.substring(0, j);
                File file = D2(s);
                if(file == null)
                    throw new IOException(getResString("ErrorCreatingNewCacheFile"));
                FileOutputStream fileoutputstream = null;
                abyte0 = readInputStream(inputstream, l, i);
                if(abyte0 == null)
                    throw new IOException(getResString("ErrorReadingInputStream"));
                fileoutputstream = new FileOutputStream(file);
                fileoutputstream.write(abyte0, 0, abyte0.length);
                fileoutputstream.close();
                String s1 = file.getPath();
                if(s1 == null)
                    throw new IOException(getResString("ErrorReadingInputStream"));
                setCacheURL(url, s1, abyte0.length, l1);
            }
        }
        catch(IOException ioexception)
        {
            throw ioexception;
        }
        return abyte0;
    }

    protected byte[] readURLLocalFile(URL url, boolean flag, long l)
        throws IOException
    {
        return null;
    }

    protected synchronized void removeCacheEntry(GenCacheEntry gencacheentry)
    {
        if(gencacheentry != null)
        {
            File file = D5((String)gencacheentry.value);
            if(file != null)
                file.delete();
            super.removeCacheEntry(gencacheentry);
        }
    }

    public void setLoad(boolean flag)
    {
        D5 = flag;
    }

    private boolean D8(String s)
    {
        s = makeJavaPretty(s);
        File file = new File(s);
        return file != null && file.exists();
    }

    protected boolean verifyCacheValue(GenCacheEntry gencacheentry)
    {
        if(gencacheentry == null)
            return false;
        else
            return D5((String)gencacheentry.value) != null;
    }

    private synchronized void D9()
    {
        if(mIndex == null || mIndex.size() == 0)
            return;
        D0(false);
        try
        {
            File file = D1();
            if(file == null)
                throw new IOException(getResString("CouldntCreateIndexFile"));
            FileWriter filewriter = new FileWriter(file);
            filewriter.write("Generator Disk Cache Index - DO NOT EDIT!\r\n");
            for(Enumeration enumeration = mIndex.keys(); enumeration.hasMoreElements();)
            {
                String s = (String)enumeration.nextElement();
                GenCacheEntry gencacheentry = (GenCacheEntry)mIndex.get(s);
                if(!isCacheEntryExpired(gencacheentry) && D8((String)gencacheentry.value))
                    filewriter.write(String.valueOf(gencacheentry.key) + "," + gencacheentry.value + "," + gencacheentry.size + "," + gencacheentry.time + "," + gencacheentry.expires + "\r\n");
            }

            filewriter.flush();
            filewriter.close();
        }
        catch(IOException ioexception)
        {
            System.out.println(ioexception);
        }
    }

    private final String D0 = "index.dat";
    private final String D1 = "Generator Disk Cache Index - DO NOT EDIT!";
    private final String D2 = "/";
    private String D3;
    private boolean D4;
    private boolean D5;
}