Created
March 4, 2023 19:03
-
-
Save david-bakin-sl/db165a3658d2eb37addb5cea1da53f4c to your computer and use it in GitHub Desktop.
shallow copy via serialization
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
private Object copy(final Object src) { | |
byte[] intermediate = null; | |
try (final var bos = new ByteArrayOutputStream(); | |
final var oos = new ObjectOutputStream(bos)) { | |
oos.writeObject(src); | |
oos.flush(); | |
intermediate = bos.toByteArray(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
try (final var bis = new ByteArrayInputStream(intermediate); | |
final var ois = new ObjectInputStream(bis)) { | |
return ois.readObject(); | |
} catch (IOException | ClassNotFoundException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment