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