code_serialization

Totally 4 files

src/main/groovy/org/cfig/learn/CommitInfo.groovy
src/main/groovy/org/cfig/learn/CommitInfo2.groovy
src/main/groovy/org/cfig/learn/Serializer.groovy
src/test/groovy/org/cfig/learn/CommitInfoTest.groovy

src/main/groovy/org/cfig/learn/CommitInfo.groovy

package org.cfig.learn

import groovy.transform.ToString

/**
 * Created by yu on 1/13/16.
 */
@ToString(includeNames = true)
class CommitInfo implements Serializable {
    private static final long serialVersionUID = 42L;

    String author;
    transient String email;
    String message;
    transient String password;

    //public CommitInfo() //unreachable

    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        println(this.class.name + ".writeObject()");
        out.defaultWriteObject();
        out.writeUTF(email);
    }

    private void readObject(java.io.ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
        println(this.class.name + ".readObject()");
        inputStream.defaultReadObject();
        email = inputStream.readUTF();
    }

}

src/main/groovy/org/cfig/learn/CommitInfo2.groovy

package org.cfig.learn

import groovy.transform.ToString

/**
 * Created by yu on 1/13/16.
 */
@ToString(includeNames = true)
class CommitInfo2 implements Externalizable {
    private static final long serialVersionUID = 5923267531712419764L;

    String author;
    transient String email;
    String message;
    transient password;

    CommitInfo2() {
        println(this.class.name + ".constructor");
    }

    @Override
    void writeExternal(ObjectOutput objectOutput) throws IOException {
        println(this.class.name + ".writeExternal()")
        objectOutput.writeObject(author)
        objectOutput.writeObject(email)
        objectOutput.writeObject(message)
        objectOutput.writeObject("***")
    }

    @Override
    void readExternal(ObjectInput objectInput)
            throws IOException, ClassNotFoundException {
        println(this.class.name + ".readExternal()")
        author = objectInput.readObject()
        email = objectInput.readObject()
        message = objectInput.readObject()
        password = "###"
    }

    //private void writeObject(java.io.ObjectOutputStream out) //unreachable
    //private void readObject(java.io.ObjectInputStream inputStream) //unreachable
}

src/main/groovy/org/cfig/learn/Serializer.groovy

package org.cfig.learn

/**
 * Created by yu on 1/13/16.
 */
class Serializer {
    public byte[] serialize(Object inObj) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(inObj);
        return baos.toByteArray();
    }

    public Object deserialize(byte[] inBytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(inBytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        return ois.readObject();
    }
    public void serialToFile(Object inObj, String inFile) {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(inFile));
        oos.writeObject(inObj);
        oos.close()
    }

    public Object deserializeFromFile(String inFile) {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(inFile));
        return ois.readObject();
    }
}

src/test/groovy/org/cfig/learn/CommitInfoTest

package org.cfig.learn

import org.junit.Before;
import org.junit.Test;

/**
 * Created by yu on 1/13/16.
 */
public class CommitInfoTest {
    CommitInfo ci = new CommitInfo();
    CommitInfo2 ci2 = new CommitInfo2();

    @Before
    public void setUp() throws Exception {
        ci.author = "good";
        ci.email = "me@good.com"
        ci.message = "some message";
        ci.password = "123456";

        ci2.author = "bad";
        ci2.email = "it@bad.com"
        ci2.message = "other message";
        ci2.password = "654321";
    }

    @Test
    public void testSerializableFile() throws Exception {
        //file
        Serializer sz = new Serializer();
        sz.serialToFile(ci, "out.file");
        def ci12 = sz.deserializeFromFile("out.file");
        println(this.class.name + ".testSerializable(), read back: " + ci12);
    }

    @Test
    public void testSerializableBytes() throws Exception {
        //bytes
        Serializer sz = new Serializer();
        byte[] aBytes = sz.serialize(ci);
        def ci13 = sz.deserialize(aBytes);
        println(this.class.name + ".testSerializable(), read back: " + ci13);
    }

    @Test
    public void testExternalizable() throws Exception {
        println("Original c2: " + ci2);
        //file
        Serializer sz = new Serializer();
        //bytes
        byte[] aBytes = sz.serialize(ci2);
        def ci21 = sz.deserialize(aBytes);
        println("Read back c2: " + ci21);
    }
}

build.gradle

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.4'
    testCompile 'junit:junit:4.12'
}