KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > Marshaller


1 // $Id: Marshaller.java,v 1.5 2004/10/04 20:43:35 belaban Exp $
2

3 package org.jgroups.util;
4
5
6 import org.jgroups.conf.ClassConfigurator;
7 import org.jgroups.ChannelException;
8
9 import java.io.Externalizable JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInput JavaDoc;
12 import java.io.ObjectOutput JavaDoc;
13
14
15
16
17 /**
18  * Title: JGroups Communications
19  * Description: Contact me at <a HREF="mailto:mail@filip.net">mail@filip.net</a>
20  * Copyright: Copyright (c) 2002
21  * Company: www.filip.net
22  * @author Filip Hanik
23  * @author Bela Ban
24  * @version 1.0
25  *
26  * This class marshalls classes, in other words it serializes and deserializes classes
27  * to and from object streams.
28  * It performs a magic number matching to decrease the number of bytes that are being sent
29  * over the wire.
30  * If no magic number is available for the class, the classname is sent over instead
31  */

32 public class Marshaller {
33     /**
34      * The class configurator maps classes to magic numbers
35      */

36     private static final ClassConfigurator mConfigurator;
37
38     static {
39         try {
40             mConfigurator=ClassConfigurator.getInstance(true);
41         }
42         catch(ChannelException e) {
43             throw new ExceptionInInitializerError JavaDoc(e.toString());
44         }
45     }
46
47
48     public Marshaller() {
49     }
50
51     /**
52      * reads the magic number, instantiates the class (from the
53      * configurator) and invokes the readExternal method on the object.
54      * If no magic number is present, the method will read the
55      * string and then get the class from the configurator.
56      * @param in an ObjectInput stream - the stream should be composed as follows:<BR>
57      * [boolean -> int|string -> object data]
58      * <BR>
59      * If the boolean is true, then the next value is an int, the magic number.<BR>
60      * If the boolean is false, then the next value is a string (the class name)<BR>
61      * The object data is what the object instance uses to populate its fields<BR>
62      */

63     public static Externalizable JavaDoc read(ObjectInput JavaDoc in) throws IOException JavaDoc {
64         try {
65             boolean is_null=in.readBoolean();
66             if(is_null)
67                 return null;
68
69             //see if we want to use a magic number
70
boolean usemagic=in.readBoolean();
71             //the class that we will use to instantiate the object with
72
Class JavaDoc extclass=null;
73             if(usemagic) {
74                 //read the magic number
75
int magic=in.readInt();
76                 //from the magic number, get the class
77
extclass=mConfigurator.get(magic);
78             }
79             else {
80                 //we don't have a magic number, read the class name
81
String JavaDoc magic=in.readUTF();
82                 //get the class, ie let the configurator load it
83
extclass=mConfigurator.get(magic);
84             }//end if
85
//instantiate the object
86
Externalizable JavaDoc newinstance=(Externalizable JavaDoc)extclass.newInstance();
87             //populate the object with its data
88
newinstance.readExternal(in);
89             //return the instance
90
return newinstance;
91         }
92         catch(Throwable JavaDoc x) {
93             if(x instanceof IOException JavaDoc)
94                 throw (IOException JavaDoc)x;
95             else
96                 throw new IOException JavaDoc(x.toString());
97         }
98     }
99
100     /**
101      * Writes an object to the ObjectOutput stream.
102      * If possible, we will send over a magic number instead of the class name
103      * so that we transfer less amount of data.
104      * @param inst - an object instance to be serialized, can not be null
105      * @param out - the ObjectOutput stream we will write the serialized data to
106      */

107     public static void write(Externalizable JavaDoc inst, ObjectOutput JavaDoc out) throws IOException JavaDoc {
108         boolean is_null=(inst == null);
109         try {
110             // if inst is a null value we write this first
111
out.writeBoolean(is_null);
112             if(is_null)
113                 return;
114
115             //find out if we have a magic number for this class
116
int magic=mConfigurator.getMagicNumber(inst.getClass());
117             //-1 means no magic number otherwise we have one
118
if(magic != -1) {
119                 //true means we use a magic number
120
out.writeBoolean(true);
121                 //write the magic number
122
out.writeInt(magic);
123             }
124             else {
125                 //we don't have a magic number
126
out.writeBoolean(false);
127                 //write the classname instead
128
out.writeUTF(inst.getClass().getName());
129             }//end if
130
//write the object data
131
inst.writeExternal(out);
132         }
133         catch(Exception JavaDoc x) {
134             if(x instanceof IOException JavaDoc)
135                 throw (IOException JavaDoc)x;
136             else
137                 throw new java.io.IOException JavaDoc(x.toString());
138         }
139     }
140
141
142 }
143
Popular Tags