Here you can find the source of serialize(Object obj)
Parameter | Description |
---|---|
obj | Object to serialize |
public static ByteBuffer serialize(Object obj)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.nio.ByteBuffer; public class Main { /**// ww w . jav a2 s. c o m * Serialize an object into a ByteBuffer. * @param obj Object to serialize * @return Buffer of the serialized object */ public static ByteBuffer serialize(Object obj) { try { //todo: make serialization less clunky! ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); byte[] b = baos.toByteArray(); oos.close(); return ByteBuffer.wrap(b); } catch (IOException e) { throw new RuntimeException(e); } } }