// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:43:28
// 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/URLCache

package com.macromedia.generator.app;

import com.macromedia.generator.platform.Mac;
import com.macromedia.generator.protocol.fgftp.FgftpURLConnection;
import java.io.*;
import java.net.*;
import java.util.Hashtable;

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

public abstract class URLCache extends GenCache
{

    public URLCache(long l, boolean flag, boolean flag1, long l1, boolean flag2, 
            int i, int j, CacheLog cachelog)
    {
        super(l, flag1, l1, cachelog);
        mForce = false;
        mCheckModifiedSince = false;
        mDiskBufferSize = 0;
        mNetBufferSize = 0;
        setBufferSize(i, j);
        setForce(flag);
        setCheckModifiedSince(flag2);
    }

    protected boolean checkModifiedSince(URL url)
        throws IOException
    {
        boolean flag = false;
        long l = 0L;
        long l1 = getCacheURLTime(url);
        if(l1 == 0L)
            return false;
        if(isLocal(url))
        {
            String s = getURLLocalPath(url);
            if(s == null)
                return false;
            File file = new File(s);
            if(file == null)
                return false;
            l = file.lastModified();
        } else
        {
            URLConnection urlconnection = null;
            try
            {
                urlconnection = url.openConnection();
                if(urlconnection instanceof HttpURLConnection)
                {
                    HttpURLConnection httpurlconnection = (HttpURLConnection)urlconnection;
                    l = httpurlconnection.getLastModified();
                }
                if(urlconnection != null && (urlconnection instanceof HttpURLConnection))
                    ((HttpURLConnection)urlconnection).disconnect();
            }
            catch(IOException ioexception)
            {
                if(urlconnection != null && (urlconnection instanceof HttpURLConnection))
                    ((HttpURLConnection)urlconnection).disconnect();
                throw ioexception;
            }
        }
        if(l > l1)
        {
            logCacheMessage(getResString("URLModified") + url.toString(), 5);
            flag = true;
        }
        return flag;
    }

    public Object fetchURLData(URL url, boolean flag, long l)
        throws IOException
    {
        if(url == null)
            return null;
        byte abyte0[] = null;
        if(isLocal(url))
        {
            try
            {
                abyte0 = readURLLocalFile(url, flag, l);
            }
            catch(IOException ioexception)
            {
                throw ioexception;
            }
        } else
        {
            URLConnection urlconnection = null;
            InputStream inputstream = null;
            try
            {
                urlconnection = url.openConnection();
                if(urlconnection == null)
                    return null;
                if(urlconnection instanceof HttpURLConnection)
                {
                    int i = ((HttpURLConnection)urlconnection).getResponseCode();
                    if(i != 200)
                        throw new IOException(((HttpURLConnection)urlconnection).getResponseMessage());
                }
                long l1 = urlconnection.getContentLength();
                if(l1 == 0L || l1 == -1L)
                    throw new IOException(getResString("InvalidURL"));
                inputstream = urlconnection.getInputStream();
                if(inputstream != null)
                {
                    long l2 = mNetBufferSize;
                    if(l2 == 0L)
                        l2 = l1;
                    abyte0 = readURLInputStream(url, inputstream, l1, (int)l2, flag, l);
                    if(abyte0 != null || !(urlconnection instanceof FgftpURLConnection))
                        inputstream.close();
                    if(abyte0 != null)
                        logCacheMessage(getResString("FetchedURL") + url.toString(), 2);
                }
                if(urlconnection != null && (urlconnection instanceof HttpURLConnection))
                    ((HttpURLConnection)urlconnection).disconnect();
            }
            catch(IOException ioexception1)
            {
                if(inputstream != null)
                    inputstream.close();
                if(urlconnection != null && (urlconnection instanceof HttpURLConnection))
                    ((HttpURLConnection)urlconnection).disconnect();
                throw ioexception1;
            }
        }
        return abyte0;
    }

    protected Object getCacheURL(URL url)
    {
        if(!isCacheEnabled())
        {
            return null;
        } else
        {
            String s = url.toString();
            return getCacheValue(s);
        }
    }

    protected synchronized GenCacheEntry getCacheURLEntry(URL url)
    {
        if(mIndex == null)
            return null;
        if(!isCacheEnabled())
        {
            return null;
        } else
        {
            String s = url.toString();
            return (GenCacheEntry)mIndex.get(s);
        }
    }

    protected long getCacheURLTime(URL url)
    {
        if(!isCacheEnabled())
            return 0L;
        GenCacheEntry gencacheentry = getCacheURLEntry(url);
        if(gencacheentry != null)
            return gencacheentry.time;
        else
            return 0L;
    }

    public boolean getCheckModifiedSince()
    {
        return mCheckModifiedSince;
    }

    public int getDiskBufferSize()
    {
        return mDiskBufferSize;
    }

    public boolean getForce()
    {
        return mForce;
    }

    public int getNetBufferSize()
    {
        return mNetBufferSize;
    }

    public Object getURLData(URL url)
        throws IOException
    {
        if(url == null)
            return null;
        Object obj = null;
        if(isCacheEnabled())
        {
            if(mCheckModifiedSince && checkModifiedSince(url))
            {
                removeCacheURL(url);
                return null;
            }
            obj = getCacheURL(url);
        }
        return obj;
    }

    protected String getURLLocalPath(URL url)
    {
        String s = null;
        s = url.toString();
        s = stripLocalPrefix(s);
        return s;
    }

    protected boolean isLocal(URL url)
    {
        String s = url.getProtocol();
        return s.equals("file");
    }

    protected String makeJavaPretty(String s)
    {
        Mac mac = Mac.getMac();
        if(mac != null)
            return mac.makeJavaPretty(s);
        else
            return s;
    }

    protected String makeSystemPretty(String s)
    {
        Mac mac = Mac.getMac();
        if(mac != null)
            return mac.makeSystemPretty(s);
        else
            return s;
    }

    protected byte[] readFile(String s)
        throws IOException
    {
        byte abyte0[] = null;
        File file = new File(makeJavaPretty(s));
        if(file == null || !file.exists())
            return null;
        try
        {
            FileInputStream fileinputstream = new FileInputStream(file);
            long l = file.length();
            if(l == 0L)
                throw new IOException(getResString("EmptyFile") + s);
            if(fileinputstream != null)
            {
                long l1 = mDiskBufferSize;
                if(l1 == 0L)
                    l1 = l;
                abyte0 = readInputStream(fileinputstream, l, (int)l1);
                fileinputstream.close();
            }
            if(abyte0 == null)
                throw new IOException(getResString("ErrorReadingInputStream"));
            if((long)abyte0.length != l)
                throw new IOException(getResString("IncompleteRead") + s);
        }
        catch(IOException ioexception)
        {
            throw ioexception;
        }
        logCacheMessage(getResString("ReadLocalFile") + s, 2);
        return abyte0;
    }

    protected byte[] readInputStream(InputStream inputstream, long l, int i)
        throws IOException
    {
        byte abyte0[] = null;
        try
        {
            int j = 0;
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            long l1 = 0L;
            if(i <= 0)
                i = 5000;
            byte abyte1[] = new byte[i];
            while(j != -1) 
            {
                l1 += j;
                j = inputstream.read(abyte1);
                if(j != -1)
                    bytearrayoutputstream.write(abyte1, 0, j);
            }
            bytearrayoutputstream.close();
            abyte0 = bytearrayoutputstream.toByteArray();
            long l2 = abyte0.length;
            if(l2 < l)
            {
                abyte0 = null;
                throw new IOException(getResString("IncompleteRead"));
            }
        }
        catch(IOException ioexception)
        {
            System.out.println(ioexception);
            throw ioexception;
        }
        return abyte0;
    }

    protected abstract byte[] readURLInputStream(URL url, InputStream inputstream, long l, int i, boolean flag, long l1)
        throws IOException;

    protected byte[] readURLLocalFile(URL url, boolean flag, long l)
        throws IOException
    {
        byte abyte0[] = null;
        String s = getURLLocalPath(url);
        if(s != null)
        {
            try
            {
                abyte0 = readFile(s);
            }
            catch(IOException ioexception)
            {
                throw ioexception;
            }
            if(abyte0 != null && isCacheEnabled() && (getForce() || flag))
                setCacheURL(url, abyte0, abyte0.length, l);
        }
        return abyte0;
    }

    protected void removeCacheURL(URL url)
    {
        if(!isCacheEnabled())
        {
            return;
        } else
        {
            String s = url.toString();
            removeCacheValue(s);
            return;
        }
    }

    public void setBufferSize(int i, int j)
    {
        if(i < 0)
            i = 0;
        mDiskBufferSize = i * 1000;
        logCacheMessage(getResString("SetDiskBufferSize") + Integer.toString(i) + "K", 2);
        if(j < 0)
            j = 0;
        mNetBufferSize = j * 1000;
        logCacheMessage(getResString("SetNetBufferSize") + Integer.toString(j) + "K", 2);
    }

    protected boolean setCacheURL(URL url, Object obj, long l, long l1)
    {
        if(!isCacheEnabled())
        {
            return false;
        } else
        {
            String s = url.toString();
            return setCacheValue(s, obj, l, l1);
        }
    }

    public void setCheckModifiedSince(boolean flag)
    {
        mCheckModifiedSince = flag;
        logCacheMessage(getResString("SetCheckModifiedSince") + (new Boolean(flag)).toString(), 2);
    }

    public void setForce(boolean flag)
    {
        mForce = flag;
        logCacheMessage(getResString("SetForce") + (new Boolean(flag)).toString(), 2);
    }

    protected String stripLocalPrefix(String s)
    {
        return s.substring(5);
    }

    protected final int kMinBufferSize = 5000;
    protected boolean mForce;
    protected boolean mCheckModifiedSince;
    protected int mDiskBufferSize;
    protected int mNetBufferSize;
}