Search results related to Serializable in Java Most examples were related to writing documents to files or reading from documents.
Let's say there's a serializable class called AppMessage.
I would like to deliver this class to a separate device through a socket as a byte[]. The machine will rebuild the received byte.
What should I do?
object java serialization
Get ready to deliver the bye:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();
...
} } finally {
try {
if (out != null) {
out.close();
}
} } catch (IOException ex) {
// Ignore exceptions that occur in close
}
try {
bos.close();
} } catch (IOException ex) {
// Ignore exceptions that occur in close
}
}
Create an object with the received byte:
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} } finally {
try {
bis.close();
} } catch (IOException ex) {
// Ignore exceptions that occur in close
}
try {
if (in != null) {
in.close();
}
} } catch (IOException ex) {
// Ignore exceptions that occur in close }
}
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
578 Understanding How to Configure Google API Key
581 PHP ssh2_scp_send fails to send files as intended
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.