BinaryRefAddr.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* BinaryRefAddr.java -- RefAddr that uses a byte array as content.
  2. Copyright (C) 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.naming;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.Arrays;
  34. /**
  35. * RefAddr that uses a byte array as content.
  36. * This can be used to reference objects that can only be represented as
  37. * byte arrays.
  38. *
  39. * @see Reference
  40. * @since 1.3
  41. * @author Mark Wielaard (mark@klomp.org)
  42. */
  43. public class BinaryRefAddr extends RefAddr
  44. {
  45. static final long serialVersionUID = -3415254970957330361L;
  46. /**
  47. * The possibly null content of this RefAddr.
  48. * Set by the constructor and returned by getContent.
  49. */
  50. private final byte[] buf;
  51. /**
  52. * Contructs a new BinaryRefAddr with the given type and content.
  53. * The complete content of the byte array is copied to a new array.
  54. */
  55. public BinaryRefAddr (String addrType, byte[] buf)
  56. {
  57. this(addrType, buf, 0, buf.length);
  58. }
  59. /**
  60. * Contructs a new BinaryRefAddr with the given type and the content
  61. * taken from the given byte array.
  62. * The content of the byte array is copied to a new array.
  63. */
  64. public BinaryRefAddr (String addrType, byte[] buf, int off, int length)
  65. {
  66. super(addrType);
  67. this.buf = new byte[length];
  68. System.arraycopy(buf, off, this.buf, 0, length);
  69. }
  70. /**
  71. * Returns the byte array contents as given to the constructor.
  72. * The returned byte array is shared with this object and other callers.
  73. * Changing the content of the buffer is discouraged and should only be
  74. * done when the byte array is locked.
  75. */
  76. public Object getContent ()
  77. {
  78. return buf;
  79. }
  80. /**
  81. * Checks if the object is a BinaryRefAddr with the same type and with the
  82. * same bytes in the content.
  83. *
  84. * @return true if the given object is an instance of BinaryRefAddr,
  85. * the addrType is the same as this addrType and the bytes of the
  86. * content are the same.
  87. */
  88. public boolean equals(Object o)
  89. {
  90. if (o instanceof BinaryRefAddr)
  91. {
  92. BinaryRefAddr refAddr = (BinaryRefAddr) o;
  93. if (this.getType().equals(refAddr.getType()))
  94. {
  95. byte[] c1 = (byte[]) this.getContent();
  96. byte[] c2 = (byte[]) refAddr.getContent();
  97. return Arrays.equals(c1, c2);
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Returns the hashCode which is the hasCode of the String returned by
  104. * <code>getType()</code> plus the hashCode of the byte array returned by
  105. * <code>getContent</code>. The hashCode of the byte array is calculated
  106. * by taking the xor of all the bytes in the array, or zero when there are
  107. * no bytes in the array.
  108. */
  109. public int hashCode()
  110. {
  111. int result = 0;
  112. byte[] b = (byte[]) getContent();
  113. for (int i=0; i < b.length; i++)
  114. result = result^b[i];
  115. return getType().hashCode() + result;
  116. }
  117. private static char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
  118. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  119. /**
  120. * Returns a String representation of the RefAddr. Only the first 32 bytes
  121. * of the content are added as hex encoded characters.
  122. * Should only be used for debugging purposes.
  123. */
  124. public String toString()
  125. {
  126. CPStringBuilder sb = new CPStringBuilder("[RefAddr type: ");
  127. sb.append(getType());
  128. sb.append(" content: 0x");
  129. byte[] b = (byte[]) getContent();
  130. for (int i=0; i < b.length && i < 32; i++)
  131. {
  132. sb.append(hex[(b[i]&0xf0)>>4]);
  133. sb.append(hex[b[i]&0x0f]);
  134. }
  135. if (b.length > 32)
  136. sb.append("...");
  137. sb.append("]");
  138. return sb.toString();
  139. }
  140. }