Android Open Source - hiddenmarble Serialization






From Project

Back to project page hiddenmarble.

License

The source code is released under:

Apache License

If you think the Android project hiddenmarble listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mygdx.hiddenmarble.utils;
//from  ww  w  .j a  v  a  2 s  .  com
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.badlogic.gdx.utils.Base64Coder;

/** Serialization/deserialization methods. */
public final class Serialization {
    private Serialization() {
    }
    
    /**
     * Deserializes an object from a Base64-encoded string.
     * 
     * @param  s the string from which the object is to be deserialized from
     * @return the deserialized object
     * @throws ClassNotFoundException if the class of the object cannot be found
     * @throws IOException if an I/O error occurs
     */
    public static Object fromString(String s)
            throws ClassNotFoundException, IOException {
        
        byte[] bytes = Base64Coder.decode(s);
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
        ObjectInputStream objectInputStream = null; 
        Object o = null;
        try {
            objectInputStream = new ObjectInputStream(byteInputStream);
            o  = objectInputStream.readObject();
        } finally {
            if (objectInputStream != null) {
                objectInputStream.close();
            }
        }
        return o;
    }

    /**
     * Serializes an object and converts the resulting byte array into a
     * Base64-encoded string.
     *  
     * @param  o the object to be serialized
     * @return the serialized object as a string
     * @throws IOException if an I/O error occurs
     */
    public static String toString(Object o) throws IOException {
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(byteOutputStream);
            objectOutputStream.writeObject(o);
        } finally {
            if (objectOutputStream != null) {
                objectOutputStream.close();
            }
        }
        byte[] bytes = byteOutputStream.toByteArray();
        return new String(Base64Coder.encode(bytes));
    }
}




Java Source Code List

com.mygdx.hiddenmarble.android.AndroidLauncher.java
com.mygdx.hiddenmarble.desktop.DesktopLauncher.java
com.mygdx.hiddenmarble.entities.AbstractDynamicEntity.java
com.mygdx.hiddenmarble.entities.AbstractEntity.java
com.mygdx.hiddenmarble.entities.Borders.java
com.mygdx.hiddenmarble.entities.DefaultBorders.java
com.mygdx.hiddenmarble.entities.DefaultMarble.java
com.mygdx.hiddenmarble.entities.DefaultMazeBox.java
com.mygdx.hiddenmarble.entities.DynamicEntity.java
com.mygdx.hiddenmarble.entities.Entity.java
com.mygdx.hiddenmarble.entities.Marble.java
com.mygdx.hiddenmarble.entities.Material.java
com.mygdx.hiddenmarble.entities.MazeBox.java
com.mygdx.hiddenmarble.entities.MazeFixtureDef.java
com.mygdx.hiddenmarble.ui.GameScreen.java
com.mygdx.hiddenmarble.ui.HiddenMarble.java
com.mygdx.hiddenmarble.ui.WorldRenderer.java
com.mygdx.hiddenmarble.utils.Assets.java
com.mygdx.hiddenmarble.utils.BodyHelper.java
com.mygdx.hiddenmarble.utils.MazeHelper.java
com.mygdx.hiddenmarble.utils.SaveState.java
com.mygdx.hiddenmarble.utils.Serialization.java
com.mygdx.hiddenmarble.utils.SpriteHelper.java
com.mygdx.hiddenmarble.world.GameWorldAdapter.java
com.mygdx.hiddenmarble.world.GameWorldListener.java
com.mygdx.hiddenmarble.world.GameWorld.java