
// IndexElem class
// ===============

import java.io.*;

public class IndexElem

/* methods include :

	getKey
	getNum
	toString
	(private) sizeOf
	read
	write
*/
{

	String key;
	int recordNumber;

	// constructor methods ..

	public IndexElem(String k, int rNum)
	{
		key = new String(k); // make a deep copy of the String
		recordNumber = rNum;
	} 

	public IndexElem()
	{
	}

	// selector methods ..
	public String getKey()
	{
		return key;
	}

	public int getNum()
	{
		return recordNumber;
	}

	// end of selectors

	public String toString()
	{
		return key + " " + recordNumber;
	}

	// input/output methods and constants

	static final int RECORD_STRING_SIZE = 15;
	static final int IE_SIZE = RECORD_STRING_SIZE + 4;

	private int sizeOf(char chArray[], int length)
	{
		int i = 0;
		while ((chArray[i] != 0) && (i < length))  i++;
		return i;
	}

	public void read(RandomAccessFile file, int recNum) throws IOException
	{
		int size;
		file.seek((long)recNum * IE_SIZE);
		recordNumber = file.readInt();
		byte b1[] = new byte [RECORD_STRING_SIZE];
		file.readFully(b1);
		key = new String (b1);

		char temp[] = key.toCharArray();
		size = sizeOf(temp,key.length());
		key = new String(temp, 0,size);
	} // end of method "read"

	private byte[] process(String str) 
	{
	int i;
	byte res[] = new byte[RECORD_STRING_SIZE];
	byte temp[] = str.getBytes();
	for (i = 0; i < RECORD_STRING_SIZE;i++)
		res[i] = '\0';
	for (i = 0; i < str.length();i++)
		res[i] = temp[i];
	return res;
	} // end of method "process"

	public void write(RandomAccessFile file, int recNum) throws IOException
	{
		file.seek((long)recNum * IE_SIZE);
		file.writeInt(recordNumber);
		byte b1[];
		if (key == null) 
			throw new IOException("trying to output unset key");
		b1 = process(key);
		file.write(b1);
	} // end of method "write"

} // end of class IndexElem

