Skip to content

Instantly share code, notes, and snippets.

@david-bakin-sl
Created March 4, 2023 19:03
Show Gist options
  • Save david-bakin-sl/db165a3658d2eb37addb5cea1da53f4c to your computer and use it in GitHub Desktop.
Save david-bakin-sl/db165a3658d2eb37addb5cea1da53f4c to your computer and use it in GitHub Desktop.
shallow copy via serialization
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