KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > compliance > serialization > support > Serializer


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.compliance.serialization.support;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16
17 /**
18  * @version $Revision: 1.4 $
19  */

20 public class Serializer
21 {
22    public byte[] serialize(Object JavaDoc object) throws IOException JavaDoc
23    {
24       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
25       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
26       oos.writeObject(object);
27       oos.close();
28       byte[] bytes = baos.toByteArray();
29       return bytes;
30    }
31
32    public Object JavaDoc deserialize(byte[] bytes) throws IOException JavaDoc, ClassNotFoundException JavaDoc
33    {
34       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
35       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
36       Object JavaDoc object = ois.readObject();
37       ois.close();
38       return object;
39    }
40 }
41
Popular Tags