/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This source file is part of SableVM.                            *
 *                                                                 *
 * See the file "LICENSE" for the copyright information and for    *
 * the terms and conditions for copying, distribution and          *
 * modification of this source file.                               *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
 * @author Patrick Latifi
 */
import java.io.*;

class SFieldInfoParser implements SParser
{
	private static SFieldInfoParser instance;

	private SFieldInfoParser()
	{
	}

	public static SFieldInfoParser getInstance()
	{
		if (instance == null)
			instance = new SFieldInfoParser();

		return instance;
	}

	public Object read(DataInputStream d) throws SClassFileException
	{
		int accessFlags;
		int nameIndex, descriptorIndex;
		SAttributes attributesField;

		accessFlags = SInputStream.
			readUnsignedShort(d, SConstants.ITEM_ACCESS_FLAGS);
		nameIndex = SInputStream.
			readUnsignedShort(d, SConstants.ITEM_NAME_INDEX);
		descriptorIndex = SInputStream.
			readUnsignedShort(d, SConstants.ITEM_DESCRIPTOR_INDEX);

		attributesField = (SAttributes)
			SAttributesParser.getInstance().read(d);

		return new SFieldInfo(accessFlags, nameIndex,
			descriptorIndex, attributesField);
	}

	public void write(DataOutputStream d, Object obj)
		throws SClassFileException
	{
		SFieldInfo fieldInfo = (SFieldInfo) obj;
		int attributesFieldCount;
		SAttributes attributesField;

		SInputStream.writeShort(d, fieldInfo.getAccessFlags());
		SInputStream.writeShort(d, fieldInfo.getNameIndex());
		SInputStream.writeShort(d, fieldInfo.getDescriptorIndex());

		try {
			SAttributesParser.getInstance().write(d,
				fieldInfo.getAttributes());
		} catch (SClassFileException e) {
			throw new SClassFileException("" + e.getMessage());
		}
	}
}
