public class ObjectFile
extends java.lang.Object
load()
to load the object file. To get a serializer for a specific object, invoke
getSerializer(Serializable, String)
. To load all objects for a specific type iterate over the serializers
with the use of getSerializerCount(String)
.final ObjectFile of = new ObjectFile("myFile.of"); // load file try { of.load(); } catch(IOException e) { System.err.println(e.getMessage()); } // file contains serializers for types "a" and "b" for(int i = 0; i < of.getSerializerCount("a"); i++) { A a = new A(); Serializer s = of.getSerializer(a, "a"); a.deserialize(s); } for(int i = 0; i < of.getSerializerCount("b"); i++) { B b = new B(); Serializer s = of.getSerializer(b, "b"); b.deserialize(s); }Save objects:
getSerializer(Serializable, String)
with the specific object to get the object's serializer.Serializable.serialize(Serializer)
with the serializer on the object to serialize its data.
Finally you only have to call save()
and all serializers that are requested will be stored in the file.final ObjectFile of = new ObjectFile("myFile.of"); // serialize a list of objects of class A for(int i = 0; i < list.size(); i++) { A a = list.get(i); Serializer s = of.getSerializer(a, "a"); a.serialize(s); } // save file try { of.save(); } catch(IOException e) { System.err.println(e.getMessage()); }Mapping:
getObject(int)
.Constructor and Description |
---|
ObjectFile(java.io.File file)
Creates a new object file.
|
ObjectFile(java.lang.String filename)
Creates a new object file.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
getObject(int serializeID)
Gets the related object from a given serialization identifier.
|
<T extends Serializable> |
getSerializer(T o,
java.lang.String serializerName)
Gets a serializer for a given object.
|
int |
getSerializerCount(java.lang.String serializerName)
Gets the number of serializers for a user specific type or section.
|
void |
incSerializerCount(java.lang.String serializerName)
Increments the number of serializers of a specific name.
|
void |
load()
Loads the objects from the specified location.
|
void |
save()
Saves the objects to the specified location.
|
void |
updateObject(int serializeID,
java.lang.Object o)
Updates the related object for a specific serialization identifier.
|
public ObjectFile(java.io.File file) throws java.lang.NullPointerException
file
- the filejava.lang.NullPointerException
- public ObjectFile(java.lang.String filename) throws java.lang.IllegalArgumentException
filename
- the file namejava.lang.IllegalArgumentException
- public final void load() throws java.io.IOException
java.io.IOException
- public final void save() throws java.io.IOException
java.io.IOException
- public final int getSerializerCount(java.lang.String serializerName)
getSerializerCount(String)
and then get a serializer for each object of a type by calling getSerializer(Serializable, String)
.serializerName
- the name of the serializergetSerializer(Serializable, String)
public final <T extends Serializable> Serializer getSerializer(T o, java.lang.String serializerName) throws java.lang.IllegalArgumentException
getSerializerCount(String)
you can get the number of serializers for a specific type
that are loaded from the file. So you can go through all serializers and get an object specific serializer by
calling getSerializer(Serializable, String)
.
final ObjectFile of = new ObjectFile("myFile.of"); // load file try { of.load(); } catch(IOException e) { System.err.println(e.getMessage()); } // file contains serializers for types "a" and "b" for(int i = 0; i < of.getSerializerCount("a"); i++) { A a = new A(); Serializer s = of.getSerializer(a, "a"); a.deserialize(s); } for(int i = 0; i < of.getSerializerCount("b"); i++) { B b = new B(); Serializer s = of.getSerializer(b, "b"); b.deserialize(s); }If you want to save a file you can request the serializer for each object you want to save by invoking
getSerializer(Serializable, String)
and then serialize the object with Serializable.serialize(Serializer)
.
After that you can call save()
to save the serializers of all objects.o
- the objectserializerName
- the name of the serializer (more generally the user defined type of the object)java.lang.IllegalArgumentException
- getObject(int)
public final java.lang.Object getObject(int serializeID)
serializeID
- the identifier of the serializerpublic final void updateObject(int serializeID, java.lang.Object o) throws java.lang.IllegalArgumentException
serializeID
- the identifier of the serializero
- the objectjava.lang.IllegalArgumentException
- public void incSerializerCount(java.lang.String serializerName)
serializerName
- the name