KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > naming > BinaryRefAddr


1 /*
2  * @(#)BinaryRefAddr.java 1.7 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.naming;
9
10 /**
11   * This class represents the binary form of the address of
12   * a communications end-point.
13   *<p>
14   * A BinaryRefAddr consists of a type that describes the communication mechanism
15   * and an opaque buffer containing the address description
16   * specific to that communication mechanism. The format and interpretation of
17   * the address type and the contents of the opaque buffer are based on
18   * the agreement of three parties: the client that uses the address,
19   * the object/server that can be reached using the address,
20   * and the administrator or program that creates the address.
21   *<p>
22   * An example of a binary reference address is an BER X.500 presentation address.
23   * Another example of a binary reference address is a serialized form of
24   * a service's object handle.
25   *<p>
26   * A binary reference address is immutable in the sense that its fields
27   * once created, cannot be replaced. However, it is possible to access
28   * the byte array used to hold the opaque buffer. Programs are strongly
29   * recommended against changing this byte array. Changes to this
30   * byte array need to be explicitly synchronized.
31   *
32   * @author Rosanna Lee
33   * @author Scott Seligman
34   * @version 1.7 03/12/19
35   *
36   * @see RefAddr
37   * @see StringRefAddr
38   * @since 1.3
39   */

40
41   /*
42   * The serialized form of a BinaryRefAddr object consists of its type
43   * name String and a byte array containing its "contents".
44   */

45
46 public class BinaryRefAddr extends RefAddr JavaDoc {
47     /**
48      * Contains the bytes of the address.
49      * This field is initialized by the constructor and returned
50      * using getAddressBytes() and getAddressContents().
51      * @serial
52      */

53     private byte[] buf = null;
54
55     /**
56       * Constructs a new instance of BinaryRefAddr using its address type and a byte
57       * array for contents.
58       *
59       * @param addrType A non-null string describing the type of the address.
60       * @param src The non-null contents of the address as a byte array.
61       * The contents of src is copied into the new BinaryRefAddr.
62       */

63     public BinaryRefAddr(String JavaDoc addrType, byte[] src) {
64     this(addrType, src, 0, src.length);
65     }
66
67     /**
68       * Constructs a new instance of BinaryRefAddr using its address type and
69       * a region of a byte array for contents.
70       *
71       * @param addrType A non-null string describing the type of the address.
72       * @param src The non-null contents of the address as a byte array.
73       * The contents of src is copied into the new BinaryRefAddr.
74       * @param offset The starting index in src to get the bytes.
75       * 0 <= offset <= src.length.
76       * @param count The number of bytes to extract from src.
77       * 0 <= count <= src.length-offset.
78       */

79     public BinaryRefAddr(String JavaDoc addrType, byte[] src, int offset, int count) {
80     super(addrType);
81     buf = new byte[count];
82     System.arraycopy(src, offset, buf, 0, count);
83     }
84
85     /**
86       * Retrieves the contents of this address as an Object.
87       * The result is a byte array.
88       * Changes to this array will affect this BinaryRefAddr's contents.
89       * Programs are recommended against changing this array's contents
90       * and to lock the buffer if they need to change it.
91       *
92       * @return The non-null buffer containing this address's contents.
93       */

94     public Object JavaDoc getContent() {
95     return buf;
96     }
97
98     
99     /**
100       * Determines whether obj is equal to this address. It is equal if
101       * it contains the same address type and their contents are byte-wise
102       * equivalent.
103       * @param obj The possibly null object to check.
104       * @return true if the object is equal; false otherwise.
105       */

106     public boolean equals(Object JavaDoc obj) {
107     if ((obj != null) && (obj instanceof BinaryRefAddr JavaDoc)) {
108         BinaryRefAddr JavaDoc target = (BinaryRefAddr JavaDoc)obj;
109         if (addrType.compareTo(target.addrType) == 0) {
110         if (buf == null && target.buf == null)
111             return true;
112         if (buf == null || target.buf == null ||
113             buf.length != target.buf.length)
114             return false;
115         for (int i = 0; i < buf.length; i++)
116             if (buf[i] != target.buf[i])
117             return false;
118         return true;
119         }
120     }
121     return false;
122     }
123     
124     /**
125       * Computes the hash code of this address using its address type and contents.
126       * Two BinaryRefAddrs have the same hash code if they have
127       * the same address type and the same contents.
128       * It is also possible for different BinaryRefAddrs to have
129       * the same hash code.
130       *
131       * @return The hash code of this address as an int.
132       */

133     public int hashCode() {
134     int hash = addrType.hashCode();
135     for (int i = 0; i < buf.length; i++) {
136         hash += buf[i]; // %%% improve later
137
}
138     return hash;
139     }
140
141     /**
142       * Generates the string representation of this address.
143       * The string consists of the address's type and contents with labels.
144       * The first 32 bytes of contents are displayed (in hexadecimal).
145       * If there are more than 32 bytes, "..." is used to indicate more.
146       * This string is meant to used for debugging purposes and not
147       * meant to be interpreted programmatically.
148       * @return The non-null string representation of this address.
149       */

150     public String JavaDoc toString(){
151     StringBuffer JavaDoc str = new StringBuffer JavaDoc("Address Type: " + addrType + "\n");
152
153     str.append("AddressContents: ");
154     for (int i = 0; i<buf.length && i < 32; i++) {
155         str.append(Integer.toHexString(buf[i]) +" ");
156     }
157     if (buf.length >= 32)
158         str.append(" ...\n");
159     return (str.toString());
160     }
161
162     /**
163      * Use serialVersionUID from JNDI 1.1.1 for interoperability
164      */

165     private static final long serialVersionUID = -3415254970957330361L;
166 }
167
Popular Tags