Last active
June 24, 2018 04:07
-
-
Save CoderJava/5c791a1ab202cf5909693e6d1465fda9 to your computer and use it in GitHub Desktop.
Serialization Helper for Nearby Connections API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SerializationHelper { | |
public static byte[] serialize(Object object) throws IOException{ | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); | |
// transform object to stream and then to a byte array | |
objectOutputStream.writeObject(object); | |
objectOutputStream.flush(); | |
objectOutputStream.close(); | |
return byteArrayOutputStream.toByteArray(); | |
} | |
public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException{ | |
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); | |
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); | |
return objectInputStream.readObject(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment