package net.sf.nfp.mini.misc;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import net.sf.nfp.mini.data.Mucus;
public class MucusUtils {
public static Vector getAll(RecordStore rms) throws RecordStoreException, IOException {
Vector registry = null;
if (rms == null || rms.getNumRecords() == 0)
registry = createDefaultsVector();
else
registry = loadFromRMS(rms);
return registry;
}
public static Vector loadFromRMS(RecordStore rms) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
Vector registry = new Vector(rms.getNumRecords());
RecordEnumeration enumeration = rms.enumerateRecords(null,null,false);
while(enumeration.hasNextElement()) {
int id = enumeration.nextRecordId();
Mucus mucus = new Mucus();
ByteArrayInputStream bin = new ByteArrayInputStream(rms.getRecord(id));
DataInputStream in = new DataInputStream(bin);
mucus.readFrom(in);
mucus.setId(id);
registry.addElement(mucus);
}
System.out.println("MucusUtils.loadFromRMS():"+registry);
return registry;
}
public static Vector createDefaultsVector() {
Vector registry;
Mucus[] defaults = new Mucus[]{
new Mucus(1, Mucus.NONE,"${mucus.none}"),
new Mucus(2, Mucus.POOR,"${mucus.poor}"),
new Mucus(3, Mucus.FINE,"${mucus.fine}"),
};
registry = new Vector(defaults.length);
for(int i=0; i<defaults.length;++i)
registry.addElement(defaults[i]);
return registry;
}
}