KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.util;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.jgroups.ChannelException;
6 import org.jgroups.conf.ClassConfigurator;
7
8 import java.io.IOException JavaDoc;
9 import java.io.ObjectOutputStream JavaDoc;
10 import java.io.ObjectStreamClass JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 /**
14  * Uses magic numbers for class descriptors
15  * @author Bela Ban
16  * @version $Id: MagicObjectOutputStream.java,v 1.4 2004/10/04 20:43:35 belaban Exp $
17  */

18 public class MagicObjectOutputStream extends ObjectOutputStream JavaDoc {
19     static ClassConfigurator conf=null;
20     static final Log log=LogFactory.getLog(MagicObjectOutputStream.class);
21
22
23     public MagicObjectOutputStream(OutputStream JavaDoc out) throws IOException JavaDoc {
24         super(out);
25         if(conf == null) {
26             try {
27                 conf=ClassConfigurator.getInstance(false);
28             }
29             catch(ChannelException e) {
30                 log.error("ClassConfigurator could not be instantiated", e);
31             }
32         }
33     }
34
35     protected void writeClassDescriptor(ObjectStreamClass JavaDoc desc) throws IOException JavaDoc {
36         int magic_num;
37         if(conf == null) {
38             super.writeInt(-1);
39             super.writeClassDescriptor(desc);
40             return;
41         }
42         magic_num=conf.getMagicNumberFromObjectStreamClass(desc);
43         super.writeInt(magic_num);
44         if(magic_num == -1) {
45             if(log.isTraceEnabled()) // todo: remove
46
log.trace("could not find magic number for '" + desc.getName() + "': writing full class descriptor");
47             super.writeClassDescriptor(desc);
48         }
49         else {
50             //if(log.isTraceEnabled())
51
// log.trace("writing descriptor (num=" + magic_num + "): " + desc.getName());
52
}
53     }
54
55 }
56
57
Popular Tags