/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 SConstantUtf8InfoParser implements SParser
{
	private static SConstantUtf8InfoParser instance;

	private SConstantUtf8InfoParser()
	{
	}

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

		return instance;
	}

	public Object read(DataInputStream d) throws SClassFileException
	{
		int len;
		byte[] bytes;

		len = SInputStream.readUnsignedShort(d, SConstants.ITEM_LENGTH);
		bytes = new byte[len];

		for (int i = 0; i < len; i++) {
			try {
				bytes[i] = (byte)d.readUnsignedByte();  	
			} catch (IOException e) {
				throw new SClassFileException(
					"Constant Utf8 Info: the " +
					"length field does not match " +
					"the number of bytes remaining."
					);
			}
		}

		return new SConstantUtf8Info(bytes);
	}

	public void write(DataOutputStream d, Object obj)
		throws SClassFileException
	{
		SConstantUtf8Info info = (SConstantUtf8Info) obj;

		SInputStream.writeShort(d, info.getLength());
		SInputStream.write(d, info.getBytes(), 0, info.getLength());
	}
}
