Skip to content

Instantly share code, notes, and snippets.

@CoderJava
Last active June 24, 2018 04:07
Show Gist options
  • Save CoderJava/5c791a1ab202cf5909693e6d1465fda9 to your computer and use it in GitHub Desktop.
Save CoderJava/5c791a1ab202cf5909693e6d1465fda9 to your computer and use it in GitHub Desktop.
Serialization Helper for Nearby Connections API
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