Last active
July 25, 2019 04:27
-
-
Save btakeya/f7dce0b7dfdd77472651a6e4d1c09d19 to your computer and use it in GitHub Desktop.
Java Serialization/Deserialization
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
import java.io.*; | |
public class A implements Serializable { | |
private static final long serialVersionUID = 100L; | |
private int value; | |
public A(int n) { | |
this.value = n; | |
} | |
public void setValue(int n) { | |
this.value = n; | |
} | |
public int getValue() { | |
return this.value; | |
} | |
@Override | |
public String toString() { | |
return String.format("A(value = %d)", this.value); | |
} | |
} |
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
import java.io.*; | |
import java.util.*; | |
public class Main { | |
private Byte[] toByteWrapperArray(byte[] byteArray) { | |
Byte[] newByteWrapperArray = new Byte[byteArray.length]; | |
for (int i = 0; i < byteArray.length; i += 1) { | |
newByteWrapperArray[i] = byteArray[i]; | |
} | |
return newByteWrapperArray; | |
} | |
private byte[] toBytePrimitiveArray(Byte[] byteWrapperArray) { | |
byte[] newBytePrimitiveArray = new byte[byteWrapperArray.length]; | |
for (int i = 0; i < byteWrapperArray.length; i += 1) { | |
newBytePrimitiveArray[i] = byteWrapperArray[i].byteValue(); | |
} | |
return newBytePrimitiveArray; | |
} | |
private <S extends Serializable> Optional<Byte[]> serialize(S serializableObject) { | |
Byte[] serialized; | |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | |
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { | |
oos.writeObject(serializableObject); | |
serialized = toByteWrapperArray(baos.toByteArray()); | |
} catch (IOException ioe) { | |
throw ioe; | |
} | |
} catch (IOException ioe) { | |
System.err.println(ioe.getMessage()); | |
serialized = null; | |
} | |
return Optional.ofNullable(serialized); | |
} | |
private <S extends Serializable> Optional<S> deserialize(String base64Encoded, Class<S> clazz) { | |
byte[] serialized = Base64.getDecoder().decode(base64Encoded); | |
return deserialize(serialized, clazz); | |
} | |
private <S extends Serializable> Optional<S> deserialize(byte[] serialized, Class<S> clazz) { | |
S deserialized = null; | |
try (ByteArrayInputStream bais = new ByteArrayInputStream(serialized)) { | |
try (ObjectInputStream ois = new ObjectInputStream(bais)) { | |
Object objectMember = ois.readObject(); | |
deserialized = (S) objectMember; | |
} catch (IOException | ClassNotFoundException ex) { | |
throw ex; | |
} | |
} catch (IOException | ClassNotFoundException ex) { | |
System.err.println(ex.getMessage()); | |
serialized = null; | |
} | |
return Optional.ofNullable(deserialized); | |
} | |
public static void main(String[] args) { | |
Main m = new Main(); | |
A a = new A(5); | |
System.out.println(a.toString()); | |
byte[] serialized = m.serialize(a) | |
.map(m::toBytePrimitiveArray) | |
.orElseThrow(() -> new RuntimeException("Failed to serialize")); | |
System.out.println(serialized); | |
A a2 = m.deserialize(serialized, A.class) | |
.orElseThrow(() -> new RuntimeException("Failed to deserialize")); | |
System.out.println(a2.toString()); | |
String base64Encoded = Base64.getEncoder().encodeToString(serialized); | |
System.out.println(base64Encoded); | |
A a3 = m.deserialize(base64Encoded, A.class) | |
.orElseThrow(() -> new RuntimeException("Failed to deserialize")); | |
System.out.println(a3.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: