Package totalcross.io
Class ObjectPDBFile
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.io.PDBFile
-
- totalcross.io.ObjectPDBFile
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ObjectPDBFile extends PDBFile
An extension to PDBFile that allows storage and retrieval of objects that implement the Storable interface. Create an ObjectPDBFile and use the addObject() method on the objects you want to store. If you want a particular object you can use loadObjectAt() to load the stored details into an object or to search through all records call resetSearch() and then loop with nextObject() until it returns false. The example below shows an example of it's use with a PDBFile of identical data:ObjectPDBFile oc = new ObjectPDBFile("Test.DATA"); MyObject obj = new MyObject(); oc.resetSearch(); while (oc.nextObject(obj)) { // do something with obj }Here's an example using unknown data. The two sections of code save a vector containing a number of Lines, Circles, and Squares (all implementing Storable) in no particular order, then loads it back in again.
// save data ObjectPDBFile oc=new ObjectPDBFile("Test.DATA",ObjectPDBFile.CREATE); for(int i=0,size=objs.getCount();i++) oc.addObject((Storable)objs.get(i)); oc.close(); // load data ObjectPDBFile oc=new ObjectPDBFile("Test.DATA"); oc.registerClass(new Line()); oc.registerClass(new Circle()); oc.registerClass(new Square()); objs=new Vector(); oc.resetSearch(); Storable obj; while ((obj=oc.nextObject())!=null) { objs.add(obj); } oc.close();
-
-
Field Summary
Fields Modifier and Type Field Description protected ByteArrayStreambsprotected byte[]bufprotected Vectorclassesprotected intcntprotected DataStreamds-
Fields inherited from class totalcross.io.PDBFile
CREATE, CREATE_EMPTY, DB_ATTR_APPINFODIRTY, DB_ATTR_BACKUP, DB_ATTR_COPY_PREVENTION, DB_ATTR_OK_TO_INSTALL_NEWER, DB_ATTR_READ_ONLY, DB_ATTR_RESET_AFTER_INSTALL, DB_ATTR_STREAM, READ_WRITE, REC_ATTR_DELETE, REC_ATTR_DIRTY, REC_ATTR_SECRET, REC_RELEASE
-
Fields inherited from class totalcross.io.Stream
skipBuffer
-
-
Constructor Summary
Constructors Constructor Description ObjectPDBFile(java.lang.String name)Construct a new ObjectPDBFileObjectPDBFile(java.lang.String name, int type)Constructs a new ObjectPDBFile
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddObject(Storable s)Add an object to this PDBFile.voiddeleteObjectAt(int i)Delete an object from the PDBFilebytegetObjectAttribute(int i)Get attributes of an object record from the PDBFilevoidinsertObjectAt(Storable s, int i)Insert an object to this PDBFile.StorableloadObjectAt(int i)Loads an object from the PDBFile.voidloadObjectAt(Storable s, int i)Load an object from the PDBFile into the given storable.StorablenextObject()Gets the next object in the PDBFile.booleannextObject(Storable s)Gets the next object in the PDBFile and places it in the given storable.voidregisterClass(Storable s)Registers this class with the PDBFile.voidresetSearch()Resets a counter for iterating through the PDBFile.booleansetObjectAttribute(int i, byte a)Set attributes of an object record from the PDBFilevoidsetSearchIndex(int i)Sets the search counter at the given index in the PDBFile.intsize()Get the size of this PDBFile-
Methods inherited from class totalcross.io.PDBFile
addRecord, addRecord, close, delete, deleteRecord, finalize, getAttributes, getName, getRecordAttributes, getRecordCount, getRecordOffset, getRecordPos, getRecordSize, inspectRecord, listPDBs, listPDBs, readBytes, rename, resizeRecord, searchBytes, setAttributes, setRecordAttributes, setRecordOffset, setRecordPos, skipBytes, writeBytes
-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
-
-
-
-
Field Detail
-
classes
protected Vector classes
-
cnt
protected int cnt
-
buf
protected byte[] buf
-
bs
protected ByteArrayStream bs
-
ds
protected DataStream ds
-
-
Constructor Detail
-
ObjectPDBFile
public ObjectPDBFile(java.lang.String name, int type) throws IllegalArgumentIOException, FileNotFoundException, IOExceptionConstructs a new ObjectPDBFile- Parameters:
name- the name of the PDBFiletype- the mode to open the PDBFile with- Throws:
IOExceptionFileNotFoundExceptionIllegalArgumentIOException
-
ObjectPDBFile
public ObjectPDBFile(java.lang.String name) throws IllegalArgumentIOException, IOExceptionConstruct a new ObjectPDBFile- Parameters:
name- the name of the PDBFile- Throws:
IOExceptionIllegalArgumentIOException
-
-
Method Detail
-
registerClass
public void registerClass(Storable s)
Registers this class with the PDBFile. Classes must be registered before using the loadObjectAt(int) method.- Parameters:
s- an instance of the class to register. The contents are ignored.
-
addObject
public void addObject(Storable s) throws IllegalArgumentIOException, IOException
Add an object to this PDBFile.- Parameters:
s- the storable object to add- Throws:
IOExceptionIllegalArgumentIOException
-
insertObjectAt
public void insertObjectAt(Storable s, int i) throws IllegalArgumentIOException, IOException
Insert an object to this PDBFile.- Parameters:
s- the storable object to addi- the index where to insert- Throws:
IOExceptionIllegalArgumentIOException
-
loadObjectAt
public void loadObjectAt(Storable s, int i) throws IllegalArgumentIOException, IOException
Load an object from the PDBFile into the given storable. Unpredictable results will occur if the object in the PDBFile is not of the same class as the storable given. Good for when you know what each record will contain.- Parameters:
s- the object to load the data intoi- the index in the PDBFile to load from- Throws:
IOExceptionIllegalArgumentIOException
-
loadObjectAt
public Storable loadObjectAt(int i) throws IllegalArgumentIOException, IOException
Loads an object from the PDBFile. Good for when you don't know which classes are going to be in records. Note that you must call the registerClass() with each storable class before this method will work properly.- Parameters:
i- the index in the PDBFile to load from- Returns:
- the loaded object, or null if unsucessful
- Throws:
IOExceptionIllegalArgumentIOException
-
deleteObjectAt
public void deleteObjectAt(int i) throws IllegalArgumentIOException, IOExceptionDelete an object from the PDBFile- Parameters:
i- the index to delete from- Throws:
IOExceptionIllegalArgumentIOException
-
setObjectAttribute
public boolean setObjectAttribute(int i, byte a) throws IOExceptionSet attributes of an object record from the PDBFile- Parameters:
i- the index to set the attributea- the attribute to set, use the REC_ATTR_XXXX constants from PDBFile class- Returns:
- true if sucessful, false otherwise
- Throws:
IOException
-
getObjectAttribute
public byte getObjectAttribute(int i) throws IOExceptionGet attributes of an object record from the PDBFile- Parameters:
i- the index to get the attribute from- Returns:
- the record attributes
- Throws:
IOException
-
size
public int size() throws IOExceptionGet the size of this PDBFile- Returns:
- the number of records contained by it
- Throws:
IOException
-
resetSearch
public void resetSearch()
Resets a counter for iterating through the PDBFile. Should be called before iterating with nextObject().
-
setSearchIndex
public void setSearchIndex(int i)
Sets the search counter at the given index in the PDBFile.- Parameters:
i- the index to start
-
nextObject
public boolean nextObject(Storable s) throws IllegalArgumentIOException, IOException
Gets the next object in the PDBFile and places it in the given storable.- Returns:
- true if sucessful, false if the end of the PDBFile has been reached
- Throws:
IOExceptionIllegalArgumentIOException
-
nextObject
public Storable nextObject() throws IOException
Gets the next object in the PDBFile.- Returns:
- the next object, or null on error or if the end has been reached
- Throws:
IOException
-
-