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

package com.macromedia.generator.api;

import com.macromedia.generator.api.peer.BufferPeer;

// Referenced classes of package com.macromedia.generator.api:
//            GenObject, GeneratorApiException

public class Buffer extends GenObject
{

    public Buffer()
        throws GeneratorApiException
    {
        peer = BufferPeer.New();
        if(peer == 0)
            throw new GeneratorApiException("unable to create peer");
        else
            return;
    }

    protected Buffer(int i)
        throws GeneratorApiException
    {
        peer = i;
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        else
            return;
    }

    protected void finalize()
        throws Throwable
    {
        if(peer != 0)
            BufferPeer.Release(peer);
        peer = 0;
    }

    public byte[] getBuffer(int i)
        throws GeneratorApiException
    {
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int ai[] = new int[1];
        BufferPeer.GetBufferSize(peer, i, ai);
        if(ai[0] < 1)
        {
            throw new GeneratorApiException("invalid buffer");
        } else
        {
            byte abyte0[] = new byte[ai[0]];
            BufferPeer.GetBuffer(peer, i, abyte0);
            return abyte0;
        }
    }

    public int getBufferCount()
        throws GeneratorApiException
    {
        int ai[] = new int[1];
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int i = BufferPeer.GetBufferCount(peer, ai);
        if(i == 0)
            throw new GeneratorApiException("error getting buffer count");
        else
            return ai[0];
    }

    public int getPos()
        throws GeneratorApiException
    {
        int ai[] = new int[1];
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int i = BufferPeer.GetPos(peer, ai);
        if(i == 0)
            throw new GeneratorApiException("error getting buffer position");
        else
            return ai[0];
    }

    public int getSize()
        throws GeneratorApiException
    {
        int ai[] = new int[1];
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int i = BufferPeer.GetSize(peer, ai);
        if(i == 0)
            throw new GeneratorApiException("error getting buffer size");
        else
            return ai[0];
    }

    public byte[] read(int i)
        throws GeneratorApiException
    {
        int ai[] = new int[1];
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        byte abyte0[] = new byte[i];
        int j = BufferPeer.Read(peer, abyte0, i, ai);
        if(ai[0] != i)
            throw new GeneratorApiException("error reading from buffer");
        if(j == 0)
            throw new GeneratorApiException("error reading from buffer");
        else
            return abyte0;
    }

    public void release()
    {
        if(peer != 0)
            BufferPeer.Release(peer);
        peer = 0;
    }

    public void reset()
        throws GeneratorApiException
    {
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int i = BufferPeer.Reset(peer);
        if(i == 0)
            throw new GeneratorApiException("error resetting buffer");
        else
            return;
    }

    public void seek(int i)
        throws GeneratorApiException
    {
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int j = BufferPeer.Seek(peer, i);
        if(j == 0)
            throw new GeneratorApiException("error seeking in buffer");
        else
            return;
    }

    public void write(byte abyte0[])
        throws GeneratorApiException
    {
        int i = abyte0.length;
        int ai[] = new int[1];
        if(peer == 0)
            throw new GeneratorApiException("invalid peer");
        int j = BufferPeer.Write(peer, abyte0, i, ai);
        if(ai[0] != i)
            throw new GeneratorApiException("error writing to buffer");
        if(j == 0)
            throw new GeneratorApiException("error writing to buffer");
        else
            return;
    }
}