Java Utililty Methods Serialize

List of utility methods to do Serialize

Description

The list of methods to do Serialize are organized into topic(s).

Method

byte[]serializeInt2MinLE(int value)
Returns the minimum bytes needed for the given value to encoded it in little-endian format.
if (value < 0) {
    throw new IllegalArgumentException("Negative input value");
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(4);
do {
    byteStream.write(value & 0xFF);
    value >>= 8;
} while (value != 0);
...
intserializeIntLE(int value, byte[] outbuf, int offset)
serialize Int LE
outbuf[offset++] = (byte) (value);
outbuf[offset++] = (byte) (value >> 8);
outbuf[offset++] = (byte) (value >> 16);
outbuf[offset++] = (byte) (value >> 24);
return offset;
byte[]serializeJdk(Object obj)
Serializes a given object into byte array.
ByteArrayOutputStream byteOut = null;
ObjectOutputStream objOut = null;
try {
    byteOut = new ByteArrayOutputStream(512);
    objOut = new ObjectOutputStream(byteOut);
    objOut.writeObject(obj);
    objOut.flush();
    return byteOut.toByteArray();
...
ByteArrayOutputStreamserializeMock(Object mock)
serialize Mock
ByteArrayOutputStream serialized = new ByteArrayOutputStream();
new ObjectOutputStream(serialized).writeObject(mock);
return serialized;
BserializeObject(B b)
serialize Object
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(b);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (B) ois.readObject();
byte[]serializeObject(final Object object)
serialize Object
ObjectOutput out = null;
byte[] buf = null;
if (object == null) {
    return null;
try {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
...
voidserializeObject(String absFilePath, Object obj)
serialize Object
FileOutputStream fileStream = null;
ObjectOutputStream objStream = null;
try {
    fileStream = new FileOutputStream(absFilePath);
    objStream = new ObjectOutputStream(fileStream);
    objStream.writeObject(obj);
} finally {
    if (objStream != null)
...
voidserializeObject(String path, Object e)
serialize Object
OutputStream os;
try {
    os = new FileOutputStream(path);
    ObjectOutput oo = new ObjectOutputStream(os);
    oo.writeObject(e);
    oo.close();
} catch (Exception e1) {
    e1.printStackTrace();
...
byte[]serializeObject(T obj)
Seralize a Serializable object.
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bos)) {
    oo.writeObject(obj);
    oo.flush();
    return bos.toByteArray();
voidserializeObjects(File oof, Object[] objectsOut)
serialize Objects
FileOutputStream fos = new FileOutputStream(oof);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(objectsOut);
oos.close();