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