clean up some run serialization stuff (especially icons!)

previously icons got their raw pixel data serialized to a massive
int array type structure in xml. now we serialize the entire ImageIcon
object to a base64 string, which is still quite a long string, but
fits nicely on one line instead of making the resulting xml feel hugely
bloated.
This commit is contained in:
Gered 2015-12-02 15:34:21 -05:00
parent 69f2160a6e
commit 0b54228770
2 changed files with 80 additions and 0 deletions

View file

@ -345,6 +345,7 @@ final class Actions {
*/
private void xmlRead( InputStream in ) {
XStream xml = new XStream( new DomDriver() );
SerializationUtils.customize(xml);
master.setRun( ( Run ) xml.fromXML( in ) );
}
@ -381,6 +382,7 @@ final class Actions {
BufferedOutputStream out = null;
try {
XStream xml = new XStream( new DomDriver() );
SerializationUtils.customize(xml);
out = new BufferedOutputStream( new FileOutputStream( file ) );
xml.toXML( master.getRun(), out );
} catch ( Exception ex ) {

View file

@ -0,0 +1,78 @@
package org.fenix.llanfair;
import com.sun.xml.internal.ws.encoding.soap.SerializationException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import org.fenix.utils.config.Configuration;
import javax.swing.*;
import java.io.*;
import java.util.Base64;
public class SerializationUtils {
/**
* Adds custom serialization settings, such as aliases and converters, to the given
* XStream serialization object.
* @param xml the XStream serialization object to add custom settings to
*/
public static void customize(XStream xml) {
xml.alias("Run", Run.class);
xml.alias("Config", Configuration.class);
xml.alias("Segment", Segment.class);
xml.registerConverter(new ImageIconConverter());
}
@SuppressWarnings("unchecked")
public static <T> T base64ToObject(String base64) throws IOException, ClassNotFoundException {
byte[] data = Base64.getDecoder().decode(base64);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
T decoded = (T)ois.readObject();
ois.close();
return decoded;
}
public static String objectToBase64(Object obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
public static class ImageIconConverter implements Converter {
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
ImageIcon icon = (ImageIcon)source;
writer.startNode("ImageIcon");
try {
writer.setValue(objectToBase64(icon));
} catch (IOException e) {
throw new SerializationException(e);
}
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
reader.moveDown();
ImageIcon icon;
try {
icon = base64ToObject(reader.getValue());
} catch (Exception e) {
throw new SerializationException(e);
}
reader.moveUp();
return icon;
}
@Override
public boolean canConvert(Class type) {
return type.equals(ImageIcon.class);
}
}
}