KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > wireformat > ObjectStreamWireFormat


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

18 package org.apache.activemq.wireformat;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 import org.apache.activemq.util.ByteArrayInputStream;
30 import org.apache.activemq.util.ByteArrayOutputStream;
31 import org.apache.activemq.util.ByteSequence;
32 import org.apache.activemq.util.ClassLoadingAwareObjectInputStream;
33
34 /**
35  * A simple implementation which uses Object Stream serialization.
36  *
37  * @version $Revision: 1.1 $
38  */

39 public class ObjectStreamWireFormat implements WireFormat {
40
41     public ByteSequence marshal(Object JavaDoc command) throws IOException JavaDoc {
42         ByteArrayOutputStream baos = new ByteArrayOutputStream();
43         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(baos);
44         marshal(command, ds);
45         ds.close();
46         return baos.toByteSequence();
47     }
48
49     public Object JavaDoc unmarshal(ByteSequence packet) throws IOException JavaDoc {
50         return unmarshal(new DataInputStream JavaDoc(new ByteArrayInputStream(packet)));
51     }
52
53     public void marshal(Object JavaDoc command, DataOutput JavaDoc ds) throws IOException JavaDoc {
54         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc((OutputStream JavaDoc)ds);
55         out.writeObject(command);
56         out.flush();
57         out.reset();
58     }
59
60     public Object JavaDoc unmarshal(DataInput JavaDoc ds) throws IOException JavaDoc {
61         try {
62             ClassLoadingAwareObjectInputStream in = new ClassLoadingAwareObjectInputStream((InputStream JavaDoc)ds);
63             Object JavaDoc command;
64             command = in.readObject();
65             in.close();
66             return command;
67         } catch (ClassNotFoundException JavaDoc e) {
68             throw (IOException JavaDoc)new IOException JavaDoc("unmarshal failed: "+e).initCause(e);
69         }
70     }
71
72     public void setVersion(int version) {
73     }
74
75     public int getVersion() {
76         return 0;
77     }
78
79 }
80
Popular Tags