From 6eb98016adbf256cda7959a1b04ceeeb41c553c4 Mon Sep 17 00:00:00 2001 From: Nicolas Drebenstedt Date: Tue, 20 Jun 2017 14:18:24 +0200 Subject: [PATCH] Make happening client serializable --- .../java/blue/happening/HappeningClient.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/blue/happening/HappeningClient.java b/service/src/main/java/blue/happening/HappeningClient.java index 11049cf..3dc1ce2 100644 --- a/service/src/main/java/blue/happening/HappeningClient.java +++ b/service/src/main/java/blue/happening/HappeningClient.java @@ -3,8 +3,17 @@ import android.os.Parcel; import android.os.Parcelable; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.io.Serializable; -public class HappeningClient implements Parcelable { + +public class HappeningClient implements Parcelable, Serializable { private String uuid; private String name; @@ -46,6 +55,25 @@ public void readFromParcel(Parcel in) { name = in.readString(); } + public static HappeningClient fromBytes(byte[] bytes) { + try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + ObjectInput in = new ObjectInputStream(bis)) { + return (HappeningClient) in.readObject(); + } catch (IOException | ClassNotFoundException e) { + return null; + } + } + + public byte[] toBytes() { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutput out = new ObjectOutputStream(bos)) { + out.writeObject(this); + return bos.toByteArray(); + } catch (IOException e) { + return null; + } + } + public String getUuid() { return uuid; }