public static < T extends Serializable > byte [ ] serializeIntoBytes ( T obj ) throws IOException { try ( ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; ObjectOutputStream oos = new ObjectOutputStream ( bos ) ) { oos . writeObject ( obj ) ; oos . flush ( ) ; return bos . toByteArray ( ) ; } }
Serialize an object into a byte array .