package com.jscape.util.io.primitive;

import com.jscape.util.Base64Format;

import java.io.*;

/**
 * @author Alex Usun
 */
public class SerializableFormat {

    public static String format(Serializable data)
            throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer);
        out.writeObject(data);
        out.close();

        return Base64Format.formatData(buffer.toByteArray());
    }

    public static Serializable parse(String data)
            throws Exception {
        byte[] binaryData = Base64Format.parseData(data);
        ByteArrayInputStream buffer = new ByteArrayInputStream(binaryData);
        ObjectInputStream in = new ObjectInputStream(buffer);

        return (Serializable) in.readObject();
    }

    private SerializableFormat() {
    }
}
