OSDN Git Service

Add license clarification.
[pf3gnuchains/gcc-fork.git] / libjava / javax / naming / BinaryRefAddr.java
1 /* 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 java.util.Arrays;
41
42 /**
43  * RefAddr that uses a byte array as content.
44  * This can be used to reference objects that can only be represented as
45  * byte arrays.
46  *
47  * @see Reference
48  * @since 1.3
49  * @author Mark Wielaard (mark@klomp.org)
50  */
51 public class BinaryRefAddr extends RefAddr
52 {
53
54   /**
55    * The possibly null content of this RefAddr.
56    * Set by the constructor and returned by getContent.
57    */
58   private final byte[] buf;
59
60   /**
61    * Contructs a new BinaryRefAddr with the given type and content.
62    * The complete content of the byte array is copied to a new array.
63    */
64   public BinaryRefAddr (String addrType, byte[] buf)
65   {
66     this(addrType, buf, 0, buf.length);
67   }
68
69   /**
70    * Contructs a new BinaryRefAddr with the given type and the content
71    * taken from the given byte array.
72    * The content of the byte array is copied to a new array.
73    */
74   public BinaryRefAddr (String addrType, byte[] buf, int off, int length)
75   {
76     super(addrType);
77     this.buf = new byte[length];
78     System.arraycopy(buf, off, this.buf, 0, length);
79   }
80
81   /**
82    * Returns the byte array contents as given to the constructor.
83    * The returned byte array is shared with this object and other callers.
84    * Changing the content of the buffer is discouraged and should only be
85    * done when the byte array is locked.
86    */
87   public Object getContent ()
88   {
89     return buf;
90   }
91
92   /**
93    * Checks if the object is a BinaryRefAddr with the same type and with the
94    * same bytes in the content.
95    *
96    * @return true if the given object is an instance of BinaryRefAddr,
97    *         the addrType is the same as this addrType and the bytes of the
98    *         content are the same.
99    */
100   public boolean equals (Object o)
101   {
102     if (o instanceof BinaryRefAddr)
103       {
104         BinaryRefAddr refAddr = (BinaryRefAddr) o;
105         if (this.getType().equals(refAddr.getType()))
106           {
107             byte[] c1 = (byte[]) this.getContent();
108             byte[] c2 = (byte[]) refAddr.getContent();
109             return Arrays.equals(c1, c2);
110           }
111       }
112     return false;
113   }
114
115   /**
116    * Returns the hashCode which is the hasCode of the String returned by
117    * <code>getType()</code> plus the hashCode of the byte array returned by
118    * <code>getContent</code>. The hashCode of the byte array is calculated
119    * by taking the xor of all the bytes in the array, or zero when there are
120    * no bytes in the array.
121    */
122   public int hashCode()
123   {
124     int result = 0;
125     byte[] b = (byte[]) getContent();
126     for (int i=0; i < b.length; i++)
127       result = result^b[i];
128
129     return getType().hashCode() + result;
130   }
131
132   private static char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
133                                '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
134   /**
135    * Returns a String representation of the RefAddr. Only the first 32 bytes
136    * of the content are added as hex encoded characters.
137    * Should only be used for debugging purposes.
138    */
139   public String toString()
140   {
141     StringBuffer sb = new StringBuffer("[RefAddr type: ");
142     sb.append(getType());
143     sb.append(" content: 0x");
144     byte[] b = (byte[]) getContent();
145     for (int i=0; i < b.length && i < 32; i++)
146       {
147         sb.append(hex[(b[i]&0xf0)>>4]);
148         sb.append(hex[b[i]&0x0f]);
149       }
150     if (b.length > 32)
151       sb.append("...");
152     sb.append("]");
153     return sb.toString();
154   }
155 }