
import javax.microedition.rms.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;

public class Record {

	private String filename = "record";
	private RecordStore recordstore = null;

	public void writeNote(String name, int note) {
		try {
			recordstore = RecordStore.openRecordStore(filename, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Note neu = new Note(name, note);
		Vector recorde = new Vector();

		try {
			byte[] byteInputData = new byte[100];
			ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
			DataInputStream inputDataStream = new DataInputStream(inputStream);
			for (int i = 1; i <= recordstore.getNumRecords(); i++) {
				recordstore.getRecord(i, byteInputData, 0);
				recorde.addElement(new Note(inputDataStream.readUTF(), inputDataStream.readInt()));
				inputStream.reset();
			}
			recordstore.closeRecordStore();
			RecordStore.deleteRecordStore(filename);
			recordstore = RecordStore.openRecordStore(filename, true);
			inputStream.close();
			inputDataStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		Vector temp = new Vector();
		if (recorde.size() > 0) {
			boolean insert = false;
			for (int i = 0; i < recorde.size(); i++) {
				if (note <= ((Note) recorde.elementAt(i)).note) {
					temp.addElement(recorde.elementAt(i));
				} else {
					temp.addElement(neu);
					insert = true;
					for (int j = i; j < recorde.size(); j++) {
						temp.addElement(recorde.elementAt(j));
					}
					break;
				}
			}
			if (!insert) {
				temp.addElement(neu);
			}
		} else {
			temp.addElement(neu);
		}
		try {
			byte[] outputRecord;
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			DataOutputStream outputDataStream = new DataOutputStream(outputStream);

			for (int i = 0; i < temp.size(); i++) {

				if (i > 5) {
					break;
				}
				outputDataStream.writeUTF(((Note) temp.elementAt(i)).name);
				outputDataStream.writeInt(((Note) temp.elementAt(i)).note);
				outputDataStream.flush();
				outputRecord = outputStream.toByteArray();
				recordstore.addRecord(outputRecord, 0, outputRecord.length);
				outputStream.reset();
			}
			outputStream.close();
			outputDataStream.close();
			recordstore.closeRecordStore();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Vector readNote() {
		try {
			recordstore = RecordStore.openRecordStore(filename, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Vector recorde = new Vector();
		try {
			byte[] byteInputData = new byte[100];
			ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
			DataInputStream inputDataStream = new DataInputStream(inputStream);
			for (int i = 1; i <= recordstore.getNumRecords(); i++) {
				recordstore.getRecord(i, byteInputData, 0);
				recorde.addElement(new Note(inputDataStream.readUTF(), inputDataStream.readInt()));
				inputStream.reset();
			}
			inputStream.close();
			inputDataStream.close();
			recordstore.closeRecordStore();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return recorde;
	}
}

