package be.nikiroo.utils.serial; import java.io.NotSerializableException; import java.util.HashMap; import java.util.Map; import be.nikiroo.utils.StringUtils; /** * A simple class to serialise objects to {@link String}. *
* This class does not support inner classes (it does support nested classes,
* though).
*
* @author niki
*/
public class Exporter {
private Map
* Important: If the operation fails (with a
* {@link NotSerializableException}), the {@link Exporter} will be corrupted
* (will contain bad, most probably not importable data).
*
* @param o
* the object to serialise
* @return this (for easier appending of multiple values)
*
* @throws NotSerializableException
* if the object cannot be serialised (in this case, the
* {@link Exporter} can contain bad, most probably not
* importable data)
*/
public Exporter append(Object o) throws NotSerializableException {
SerialUtils.append(builder, o, map);
return this;
}
/**
* Clear the current content.
*/
public void clear() {
builder.setLength(0);
map.clear();
}
/**
* The exported items in a serialised form.
*
* @param zip
* TRUE to have zipped content, FALSE to have raw content, NULL
* to let the system decide
*
* @return the items currently in this {@link Exporter}
*/
public String toString(Boolean zip) {
if (zip == null) {
zip = builder.length() > 128;
}
if (zip) {
return "ZIP:" + StringUtils.zip64(builder.toString());
}
return builder.toString();
}
/**
* The exported items in a serialised form (possibly zipped).
*
* @return the items currently in this {@link Exporter}
*/
@Override
public String toString() {
return toString(null);
}
}