KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > mux > MuxHeader


1 package org.jgroups.mux;
2
3 import org.jgroups.Global;
4 import org.jgroups.Header;
5 import org.jgroups.util.Streamable;
6 import org.jgroups.util.Util;
7
8 import java.io.*;
9
10 /**
11  * Header used for multiplexing and de-multiplexing between service components on top of a Multiplexer (Channel)
12  * @author Bela Ban
13  * @version $Id: MuxHeader.java,v 1.7 2007/05/01 10:55:18 belaban Exp $
14  */

15 public class MuxHeader extends Header implements Streamable {
16     String JavaDoc id=null;
17
18     /** Used for service state communication between Multiplexers */
19     ServiceInfo info;
20
21     public MuxHeader() {
22     }
23
24     public MuxHeader(String JavaDoc id) {
25         this.id=id;
26     }
27
28     public MuxHeader(ServiceInfo info) {
29         this.info=info;
30     }
31
32     public String JavaDoc getId() {
33         return id;
34     }
35
36     public void writeExternal(ObjectOutput out) throws IOException {
37         out.writeUTF(id);
38         out.writeObject(info);
39     }
40
41     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
42         id=in.readUTF();
43         info=(ServiceInfo)in.readObject();
44     }
45
46
47     public int size() {
48         int retval=Global.BYTE_SIZE; // presence byte in Util.writeString
49
if(id != null)
50             retval+=id.length() +2; // for UTF
51
retval+=Global.BYTE_SIZE; // presence for info
52
if(info != null)
53             retval+=info.size();
54         return retval;
55     }
56
57     public void writeTo(DataOutputStream out) throws IOException {
58         Util.writeString(id, out);
59         if(info != null) {
60             out.writeBoolean(true);
61             info.writeTo(out);
62         }
63         else {
64             out.writeBoolean(false);
65         }
66     }
67
68     public void readFrom(DataInputStream in) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
69         id=Util.readString(in);
70         if(in.readBoolean()) {
71             info=new ServiceInfo();
72             info.readFrom(in);
73         }
74     }
75
76     public String JavaDoc toString() {
77         if(id != null)
78             return id;
79         if(info != null)
80             return info.toString();
81         return "";
82     }
83 }
84
Popular Tags