1. /*
  2. * @(#)BinaryRefAddr.java 1.4 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.naming;
  11. /**
  12. * This class represents the binary form of the address of
  13. * a communications end-point.
  14. *<p>
  15. * A BinaryRefAddr consists of a type that describes the communication mechanism
  16. * and an opaque buffer containing the address description
  17. * specific to that communication mechanism. The format and interpretation of
  18. * the address type and the contents of the opaque buffer are based on
  19. * the agreement of three parties: the client that uses the address,
  20. * the object/server that can be reached using the address,
  21. * and the administrator or program that creates the address.
  22. *<p>
  23. * An example of a binary reference address is an BER X.500 presentation address.
  24. * Another example of a binary reference address is a serialized form of
  25. * a service's object handle.
  26. *<p>
  27. * A binary reference address is immutable in the sense that its fields
  28. * once created, cannot be replaced. However, it is possible to access
  29. * the byte array used to hold the opaque buffer. Programs are strongly
  30. * recommended against changing this byte array. Changes to this
  31. * byte array need to be explicitly synchronized.
  32. *
  33. * @author Rosanna Lee
  34. * @author Scott Seligman
  35. * @version 1.4 00/02/02
  36. *
  37. * @see RefAddr
  38. * @see StringRefAddr
  39. * @since 1.3
  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. public class BinaryRefAddr extends RefAddr {
  46. /**
  47. * Contains the bytes of the address.
  48. * This field is initialized by the constructor and returned
  49. * using getAddressBytes() and getAddressContents().
  50. * @serial
  51. */
  52. private byte[] buf = null;
  53. /**
  54. * Constructs a new instance of BinaryRefAddr using its address type and a byte
  55. * array for contents.
  56. *
  57. * @param addrType A non-null string describing the type of the address.
  58. * @param src The non-null contents of the address as a byte array.
  59. * The contents of src is copied into the new BinaryRefAddr.
  60. */
  61. public BinaryRefAddr(String addrType, byte[] src) {
  62. this(addrType, src, 0, src.length);
  63. }
  64. /**
  65. * Constructs a new instance of BinaryRefAddr using its address type and
  66. * a region of a byte array for contents.
  67. *
  68. * @param addrType A non-null string describing the type of the address.
  69. * @param src The non-null contents of the address as a byte array.
  70. * The contents of src is copied into the new BinaryRefAddr.
  71. * @param offset The starting index in src to get the bytes.
  72. * 0 <= offset <= src.length.
  73. * @param count The number of bytes to extract from src.
  74. * 0 <= count <= src.length-offset.
  75. */
  76. public BinaryRefAddr(String addrType, byte[] src, int offset, int count) {
  77. super(addrType);
  78. buf = new byte[count];
  79. System.arraycopy(src, offset, buf, 0, count);
  80. }
  81. /**
  82. * Retrieves the contents of this address as an Object.
  83. * The result is a byte array.
  84. * Changes to this array will affect this BinaryRefAddr's contents.
  85. * Programs are recommended against changing this array's contents
  86. * and to lock the buffer if they need to change it.
  87. *
  88. * @return The non-null buffer containing this address's contents.
  89. */
  90. public Object getContent() {
  91. return buf;
  92. }
  93. /**
  94. * Determines whether obj is equal to this address. It is equal if
  95. * it contains the same address type and their contents are byte-wise
  96. * equivalent.
  97. * @param obj The possibly null object to check.
  98. * @return true if the object is equal; false otherwise.
  99. */
  100. public boolean equals(Object obj) {
  101. if ((obj != null) && (obj instanceof BinaryRefAddr)) {
  102. BinaryRefAddr target = (BinaryRefAddr)obj;
  103. if (addrType.compareTo(target.addrType) == 0) {
  104. if (buf == null && target.buf == null)
  105. return true;
  106. if (buf == null || target.buf == null ||
  107. buf.length != target.buf.length)
  108. return false;
  109. for (int i = 0; i < buf.length; i++)
  110. if (buf[i] != target.buf[i])
  111. return false;
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. /**
  118. * Computes the hash code of this address using its address type and contents.
  119. * Two BinaryRefAddrs have the same hash code if they have
  120. * the same address type and the same contents.
  121. * It is also possible for different BinaryRefAddrs to have
  122. * the same hash code.
  123. *
  124. * @return The hash code of this address as an int.
  125. */
  126. public int hashCode() {
  127. int hash = addrType.hashCode();
  128. for (int i = 0; i < buf.length; i++) {
  129. hash += buf[i]; // %%% improve later
  130. }
  131. return hash;
  132. }
  133. /**
  134. * Generates the string representation of this address.
  135. * The string consists of the address's type and contents with labels.
  136. * The first 32 bytes of contents are displayed (in hexadecimal).
  137. * If there are more than 32 bytes, "..." is used to indicate more.
  138. * This string is meant to used for debugging purposes and not
  139. * meant to be interpreted programmatically.
  140. * @return The non-null string representation of this address.
  141. */
  142. public String toString(){
  143. StringBuffer str = new StringBuffer("Address Type: " + addrType + "\n");
  144. str.append("AddressContents: ");
  145. for (int i = 0; i<buf.length && i < 32; i++) {
  146. str.append(Integer.toHexString(buf[i]) +" ");
  147. }
  148. if (buf.length >= 32)
  149. str.append(" ...\n");
  150. return (str.toString());
  151. }
  152. /**
  153. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  154. */
  155. private static final long serialVersionUID = -3415254970957330361L;
  156. }