// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.DatatypeConverter;

public class SerFun {
    public static String anySerialize(Object o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
        return DatatypeConverter.printBase64Binary(baos.toByteArray());
    }
    public static Object anyDeserialize(String s) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bais = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(s));
        ObjectInputStream ois = new ObjectInputStream(bais);
        Object o = ois.readObject();
        ois.close();
        return o;
    }
    public static void main(String[] args) throws Exception {
        List<Data> lst = new ArrayList<Data>();
        lst.add(new Data(1, "A"));
        lst.add(new Data(2, "BB"));
        lst.add(new Data(3, "CCC"));
        System.out.println(lst);
        String s = anySerialize(lst);
        @SuppressWarnings("unchecked")
        List<Data> lst2 = (List<Data>)anyDeserialize(s);
        System.out.println(lst2);
    }
}
