001 /* BinaryRefAddr.java -- RefAddr that uses a byte array as content.
002 Copyright (C) 2001 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package javax.naming;
039
040 import java.util.Arrays;
041
042 /**
043 * RefAddr that uses a byte array as content.
044 * This can be used to reference objects that can only be represented as
045 * byte arrays.
046 *
047 * @see Reference
048 * @since 1.3
049 * @author Mark Wielaard (mark@klomp.org)
050 */
051 public class BinaryRefAddr extends RefAddr
052 {
053 static final long serialVersionUID = -3415254970957330361L;
054
055 /**
056 * The possibly null content of this RefAddr.
057 * Set by the constructor and returned by getContent.
058 */
059 private final byte[] buf;
060
061 /**
062 * Contructs a new BinaryRefAddr with the given type and content.
063 * The complete content of the byte array is copied to a new array.
064 */
065 public BinaryRefAddr (String addrType, byte[] buf)
066 {
067 this(addrType, buf, 0, buf.length);
068 }
069
070 /**
071 * Contructs a new BinaryRefAddr with the given type and the content
072 * taken from the given byte array.
073 * The content of the byte array is copied to a new array.
074 */
075 public BinaryRefAddr (String addrType, byte[] buf, int off, int length)
076 {
077 super(addrType);
078 this.buf = new byte[length];
079 System.arraycopy(buf, off, this.buf, 0, length);
080 }
081
082 /**
083 * Returns the byte array contents as given to the constructor.
084 * The returned byte array is shared with this object and other callers.
085 * Changing the content of the buffer is discouraged and should only be
086 * done when the byte array is locked.
087 */
088 public Object getContent ()
089 {
090 return buf;
091 }
092
093 /**
094 * Checks if the object is a BinaryRefAddr with the same type and with the
095 * same bytes in the content.
096 *
097 * @return true if the given object is an instance of BinaryRefAddr,
098 * the addrType is the same as this addrType and the bytes of the
099 * content are the same.
100 */
101 public boolean equals(Object o)
102 {
103 if (o instanceof BinaryRefAddr)
104 {
105 BinaryRefAddr refAddr = (BinaryRefAddr) o;
106 if (this.getType().equals(refAddr.getType()))
107 {
108 byte[] c1 = (byte[]) this.getContent();
109 byte[] c2 = (byte[]) refAddr.getContent();
110 return Arrays.equals(c1, c2);
111 }
112 }
113 return false;
114 }
115
116 /**
117 * Returns the hashCode which is the hasCode of the String returned by
118 * <code>getType()</code> plus the hashCode of the byte array returned by
119 * <code>getContent</code>. The hashCode of the byte array is calculated
120 * by taking the xor of all the bytes in the array, or zero when there are
121 * no bytes in the array.
122 */
123 public int hashCode()
124 {
125 int result = 0;
126 byte[] b = (byte[]) getContent();
127 for (int i=0; i < b.length; i++)
128 result = result^b[i];
129
130 return getType().hashCode() + result;
131 }
132
133 private static char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
134 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
135 /**
136 * Returns a String representation of the RefAddr. Only the first 32 bytes
137 * of the content are added as hex encoded characters.
138 * Should only be used for debugging purposes.
139 */
140 public String toString()
141 {
142 StringBuffer sb = new StringBuffer("[RefAddr type: ");
143 sb.append(getType());
144 sb.append(" content: 0x");
145 byte[] b = (byte[]) getContent();
146 for (int i=0; i < b.length && i < 32; i++)
147 {
148 sb.append(hex[(b[i]&0xf0)>>4]);
149 sb.append(hex[b[i]&0x0f]);
150 }
151 if (b.length > 32)
152 sb.append("...");
153 sb.append("]");
154 return sb.toString();
155 }
156 }