KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > ObjectMarshaller


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
4  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
5  * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  */

14
15 package org.apache.activemq.kaha;
16
17 import java.io.ByteArrayInputStream JavaDoc;
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.DataInput JavaDoc;
20 import java.io.DataOutput JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24
25 /**
26  * Implementation of a Marshaller for Objects
27  *
28  * @version $Revision: 1.2 $
29  */

30 public class ObjectMarshaller implements Marshaller{
31
32     /**
33      * Write the payload of this entry to the RawContainer
34      *
35      * @param object
36      * @param dataOut
37      * @throws IOException
38      */

39     public void writePayload(Object JavaDoc object,DataOutput JavaDoc dataOut) throws IOException JavaDoc{
40         ByteArrayOutputStream JavaDoc bytesOut=new ByteArrayOutputStream JavaDoc();
41         ObjectOutputStream JavaDoc objectOut=new ObjectOutputStream JavaDoc(bytesOut);
42         objectOut.writeObject(object);
43         objectOut.close();
44         byte[] data=bytesOut.toByteArray();
45         dataOut.writeInt(data.length);
46         dataOut.write(data);
47     }
48
49     /**
50      * Read the entry from the RawContainer
51      *
52      * @param dataIn
53      * @return unmarshalled object
54      * @throws IOException
55      */

56     public Object JavaDoc readPayload(DataInput JavaDoc dataIn) throws IOException JavaDoc{
57         int size=dataIn.readInt();
58         byte[] data=new byte[size];
59         dataIn.readFully(data);
60         ByteArrayInputStream JavaDoc bytesIn=new ByteArrayInputStream JavaDoc(data);
61         ObjectInputStream JavaDoc objectIn=new ObjectInputStream JavaDoc(bytesIn);
62         try{
63             return objectIn.readObject();
64         }catch(ClassNotFoundException JavaDoc e){
65             throw new IOException JavaDoc(e.getMessage());
66         }
67     }
68 }
69
Popular Tags