package com.flagstone.transform;

import java.io.IOException;

/**
 * This exception is thrown when parse errors are encountered. ParseExceptions
 * contain information about the error that prevented a file from being read and
 * decoded.
 */
public class FSCoderException extends IOException
{
	private int type = 0;
	private int start = 0;
	private int length = 0;
	private int delta = 0;

	/**
	 * Constructs an FSCoderException to report where a problem occurred when
	 * encoding or decoding a Flash (.swf) file.
	 * 
	 * @param type
	 *            the type of object or action that was being encoded/decoded
	 *            when the problem occurred.
	 * 
	 * @param start
	 *            the address in the file where the data structure being
	 *            encoded/decoded is located. This is only valid for files being
	 *            decoded since the encoded file will not be written if an
	 *            exception occurs.
	 * 
	 * @param length
	 *            the size in bytes that the data structure should take when
	 *            encoded.
	 * 
	 * @param delta
	 *            the difference between the actual number of byes and the
	 *            expected length. This is negative for underflow errors and
	 *            positive when the takes more bytes to encode/decode that the
	 *            expected length.
	 * 
	 * @param message
	 *            a message indicating the type of error - overflow or
	 *            underflow.
	 */
	public FSCoderException(int type, int start, int length, int delta, String message)
	{
		super(message);
		this.type = type;
		this.start = start;
		this.length = length;
		this.delta = delta;
	}

	/**
	 * Returns the type identifying the FSMovieObject or FSActionObject that
	 * caused the error.
	 * 
	 * @return the object type.
	 */
	public int getType()
	{
		return type;
	}

	/**
	 * The byte address of the start of the object in the file being decoded.
	 * This address is not valid when encoding a Flash file since the file will
	 * probably not be encoded - though this is under the control of the
	 * software using Transform.
	 * 
	 * @return the address (offset) in bytes where the object is encoded.
	 */
	public int getStart()
	{
		return start;
	}

	/**
	 * The calculated number of bytes the object will occupy when encoded.
	 * 
	 * @return the length in bytes that the object is expected to occupy when
	 *         encoded.
	 * 
	 */
	public int getLength()
	{
		return length;
	}

	/**
	 * The difference between the calculated number of bytes and the actual
	 * number of bytes when the object was encoded or decoded.
	 * 
	 * @return the difference between the actual and expected number of bytes
	 *         encoded/decoded.
	 */
	public int getDelta()
	{
		return delta;
	}

	/**
	 * Return a description of the error.
	 * 
	 * @return a string describing the error that caused the exception.
	 */
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();

		buffer.append("FSCoderException: { ");
		buffer.append("type = ").append(type).append(", ");
		buffer.append("location = ").append(start).append(", ");
		buffer.append("length = ").append(length).append(", ");
		buffer.append("delta = ").append(delta).append(", ");
		buffer.append("message = \"").append(getMessage()).append("\" }");

		return buffer.toString();
	}
}
