blob: b5b22994d1705f617d11b4c03cbddbee9c0be02f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package derms.io;
import java.io.*;
import java.nio.ByteBuffer;
public class Serial {
public static ByteBuffer encode(Serializable obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
ByteBuffer buf = ByteBuffer.wrap(baos.toByteArray());
oos.close();
return buf;
}
public static <T extends Serializable> T decode(ByteBuffer buf, Class<T> clazz) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(buf.array()));
T obj = clazz.cast(ois.readObject());
ois.close();
return obj;
}
}
|