/*
 * FSUnknownAction.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * FSUnknownAction class is used to represent any action decoded from a Flash
 * file that is currently not supported by the framework.
 * 
 * <p>
 * This allows Flash files to be parsed, processed and encoded without affecting
 * any of the actions not directly supported.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>The value used to identify the type of action. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>data</td>
 * <td>The encoded bytes that make up the body of the action's data structure.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The object provides direct access to the encoded data of the action offering
 * the possibility to encode and decode actions that are not directly supported
 * in the current release of the framework. However a detailed knowledge of how
 * the action is encoded and decoded is required.
 * </p>
 */
public class FSUnknownAction extends FSActionObject
{
	private byte[] data = null;

	/**
	 * Construct an FSUnknownAction object, initialising it with values decoded
	 * from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSUnknownAction(FSCoder coder)
	{
		super(0);
		decode(coder);
	}

	/**
	 * Constructs an FSUnknownAction object of the specified type with the
	 * encoded Flash data.
	 * 
	 * @param aType
	 *            the code identifying the action type.
	 * @param bytes
	 *            the encoded data for the action.
	 */
	public FSUnknownAction(int aType, byte[] bytes)
	{
		super(aType);
		data = bytes;
	}

	/**
	 * Constructs an FSUnknownAction object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSUnknownAction object.
	 */
	public FSUnknownAction(FSUnknownAction obj)
	{
		super(obj);

		if (obj.data != null)
			data = Transform.clone(obj.data);
	}

	/**
	 * Gets the encoded data for the action.
	 * 
	 * @return an array of bytes representing the action data when encoded to a
	 *         Flash file.
	 */
	public byte[] getData()
	{
		return data;
	}

	/**
	 * Sets the encoded data for the action.
	 * 
	 * @param bytes
	 *            the encoded data for the action.
	 */
	public void setData(byte[] bytes)
	{
		data = bytes;
	}

	public Object clone()
	{
		FSUnknownAction anObject = (FSUnknownAction) super.clone();

		anObject.data = Transform.clone(data);

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSUnknownAction typedObject = (FSUnknownAction) anObject;

			result = Transform.equals(data, typedObject.data);
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += data.length;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);
		coder.writeBytes(data);
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);

		if (length > 0)
		{
			data = new byte[length];
			coder.readBytes(data);
		} else
			data = new byte[]
			{};

		coder.endObject(name());
	}
}
