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

package com.macromedia.generator.app;

import com.macromedia.generator.api.Buffer;
import com.macromedia.generator.api.GeneratorApiException;
import java.io.*;

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

public class RequestCache extends GenCache
{

    public RequestCache(long l, boolean flag, boolean flag1, long l1, CacheLog cachelog)
    {
        super(l, flag1, l1, cachelog);
        mForce = false;
        setForce(flag);
    }

    protected boolean addRequest(String s, Buffer buffer, boolean flag, long l)
    {
        if(isCacheEnabled() && (getForce() || flag))
        {
            RequestCacheValue requestcachevalue = new RequestCacheValue();
            if(requestcachevalue == null)
                return false;
            byte abyte0[] = FF(buffer);
            if(abyte0 == null)
                return false;
            requestcachevalue._fld0100 = s;
            requestcachevalue._fld0101 = abyte0;
            if(!verifyRequestCacheValue(requestcachevalue))
                return false;
            else
                return setCacheRequest(requestcachevalue, l);
        } else
        {
            return false;
        }
    }

    private byte[] FF(Buffer buffer)
    {
        byte abyte0[] = null;
        try
        {
            int i = buffer.getBufferCount();
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            for(int j = 0; j < i; j++)
            {
                byte abyte1[] = buffer.getBuffer(j);
                if(abyte1 != null)
                    bytearrayoutputstream.write(abyte1);
            }

            bytearrayoutputstream.close();
            abyte0 = bytearrayoutputstream.toByteArray();
        }
        catch(GeneratorApiException generatorapiexception)
        {
            logCacheMessage(generatorapiexception.toString(), 5);
            return null;
        }
        catch(IOException ioexception)
        {
            logCacheMessage(ioexception.toString(), 5);
            return null;
        }
        return abyte0;
    }

    protected RequestCacheValue getCacheRequest(String s)
    {
        if(!isCacheEnabled())
            return null;
        else
            return (RequestCacheValue)getCacheValue(s);
    }

    public boolean getForce()
    {
        return mForce;
    }

    protected Buffer getMatchingRequestBuffer(String s)
    {
        if(!isCacheEnabled())
            return null;
        RequestCacheValue requestcachevalue = getCacheRequest(s);
        if(requestcachevalue == null)
            return null;
        if(!verifyRequestCacheValue(requestcachevalue))
            return null;
        Buffer buffer = null;
        try
        {
            buffer = new Buffer();
            if(buffer == null)
                return null;
            buffer.write(requestcachevalue._fld0101);
        }
        catch(GeneratorApiException generatorapiexception)
        {
            logCacheMessage(generatorapiexception.toString(), 5);
            return null;
        }
        return buffer;
    }

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

    protected boolean setCacheRequest(RequestCacheValue requestcachevalue, long l)
    {
        if(!isCacheEnabled())
            return false;
        if(!verifyRequestCacheValue(requestcachevalue))
        {
            return false;
        } else
        {
            long l1 = requestcachevalue._fld0101.length;
            return setCacheValue(requestcachevalue._fld0100, requestcachevalue, l1, l);
        }
    }

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

    protected boolean verifyCacheValue(GenCacheEntry gencacheentry)
    {
        if(gencacheentry == null)
        {
            return false;
        } else
        {
            RequestCacheValue requestcachevalue = (RequestCacheValue)gencacheentry.value;
            return verifyRequestCacheValue(requestcachevalue);
        }
    }

    protected boolean verifyRequestCacheValue(RequestCacheValue requestcachevalue)
    {
        return requestcachevalue != null && requestcachevalue._fld0100 != null && requestcachevalue._fld0101 != null;
    }

    protected boolean mForce;
}