Java Serializable Object to Byte Array
Prepare the byte array to send:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
Create an object from a byte array:
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readObject();
}
Converting from object to byte array and back in java
This should work, just cast the object to your class after receiving it from the serialize/deserialize function.
public static byte[] objToByte(TcpPacket tcpPacket) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
objStream.writeObject(tcpPacket);
return byteStream.toByteArray();
}
public static Object byteToObj(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
ObjectInputStream objStream = new ObjectInputStream(byteStream);
return objStream.readObject();
}
How to serialize objects together with byte array into a file?
Just serialize the byte array as an object, with writeObject()
and deserialize with readObject()
. That will take care of the length automatically.
Java: how to serialize object into byte array with as few output bytes as possible?
So is there some way in Java I can serialize object/array of objects efficiently and have the output byte array to be as short as possible?
Sure. What you need to do is write your own custom serialization code. For example, using a DataOutputStream you could serialize a byte[]
like this:
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length);
and if the data was compressible then you could potentially do better.
For user-defined types, the serialization code will be more complex. But if you want "the output byte array to be as short as possible" ... then that is the price you have to pay!!
The reason that ObjectOututStream
streams are large is that it is a requirement that serialization / deserialization is type-safe; i.e. that it can detect cases where representation types have changed between serializing and deserializing. That is done by including server UUIDs and type descriptors in the stream. That takes space.
Note you (the programmer) can improve on the size of a serialized object by other means:
- by declaring unnecessary fields as
transient
, - by writing a custom
readObject
/writeObject
. or - by declaring the class to be
Externalizable
, and implementing space efficient externalization methods.
Please refer to the Java Object Serialization spec for the details.
Related Topics
Org.Xml.Sax.Saxparseexception: Content Is Not Allowed in Prolog
Something Seems Wrong With the Layout, Jbutton Showing Unexpected Behaviour At Resize of the Window
How to Set or Change the Default Java (Jdk) Version on Macos
How to Turn a List of Lists into a List in Java 8
Does Gc Release Back Memory to Os
Are Fields Initialized Before Constructor Code Is Run in Java
In Java, Is the Result of the Addition of Two Chars an Int or a Char
How to Set the Proxy to Be Used by the Jvm
How to Iterate Over a Jsonobject
How to Run Test Methods in Specific Order in Junit4
Regexp Java For Password Validation
Value Change Listener to Jtextfield
Occurrences of Substring in a String
Sort on a String That May Contain a Number
How to Secure an API Rest For Mobile App (If Sniffing Requests Gives You the "Key")