001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.naming;
019:
020: import java.util.Arrays;
021:
022: /**
023: * A <code>BinaryRefAddr</code> refers to an address which is represented by a
024: * binary address.
025: */
026: public class BinaryRefAddr extends RefAddr {
027:
028: private static final long serialVersionUID = -3415254970957330361L;
029:
030: /**
031: * The buffer for the binary address itself.
032: *
033: * @serial
034: */
035: private byte[] buf;
036:
037: /**
038: * Constructs a <code>BinaryRefAddr</code> using the specified address
039: * type and the full details of the supplied byte array.
040: *
041: * @param type
042: * the address type which cannot be null
043: * @param address
044: * the address itself which cannot be null
045: */
046: public BinaryRefAddr(String type, byte[] address) {
047: this (type, address, 0, address.length);
048: }
049:
050: /**
051: * Constructs a <code>BinaryRefAddr</code> using the specified address
052: * type and part of the supplied byte array. The number of bytes to be taken
053: * is specified by <code>size</code>. Additionally these bytes are taken
054: * from a starting point specified by <code>index</code>.
055: *
056: * @param type
057: * the address type. It cannot be null.
058: * @param address
059: * the address itself. It cannot be null.
060: * @param index
061: * the starting point to copy bytes from. It must be greater than
062: * or equal to zero and must be less than or equal to the size of
063: * the byte array.
064: * @param size
065: * the number of bytes to copy. It must be greater than or equal
066: * to zero and must be less than or equal to the size of the byte
067: * array less the starting position.
068: * @throws ArrayIndexOutOfBoundsException
069: * If <code>size</code> or <code>index</code> does not meet
070: * the constraints.
071: */
072: public BinaryRefAddr(String type, byte[] address, int index,
073: int size) {
074: super (type);
075: this .buf = new byte[size];
076: System.arraycopy(address, index, this .buf, 0, size);
077: }
078:
079: /**
080: * Gets the content of this address.
081: *
082: * @return an array of bytes containing the address. It cannot be null.
083: */
084: @Override
085: public Object getContent() {
086: return buf;
087: }
088:
089: /**
090: * Returns true if this address is equal to the supplied object
091: * <code>o</code>. They are considered equal if the address types are
092: * equal and the data in the buffers is of the same length and contains the
093: * same bytes.
094: *
095: * @param o
096: * the object to compare with
097: * @return true if this address is equal to <code>o</code>, otherwise
098: * false
099: */
100: @Override
101: public boolean equals(Object o) {
102: if (o instanceof BinaryRefAddr) {
103: BinaryRefAddr a = (BinaryRefAddr) o;
104: return this .addrType.equals(a.addrType)
105: && Arrays.equals(this .buf, a.buf);
106: }
107: return false;
108: }
109:
110: /**
111: * Returns the hashcode of this address. The result is the hashcode of the
112: * address type added to each byte from the data buffer.
113: *
114: * @return the hashcode of this address
115: */
116: @Override
117: public int hashCode() {
118: int i = this .addrType.hashCode();
119:
120: for (byte element : this .buf) {
121: i += element;
122: }
123: return i;
124: }
125:
126: /**
127: * Returns the string representation of this address. The string includes
128: * the address type and a maximum of 128 bytes address content expressed in
129: * hexadecimal form.
130: *
131: * @return the string representation of this address
132: */
133: @Override
134: public String toString() {
135: String s = "The type of the address is: " //$NON-NLS-1$
136: + this .addrType + "\nThe content of the address is: "; //$NON-NLS-1$
137: int max = this .buf.length > 128 ? 128 : this .buf.length;
138:
139: for (int i = 0; i < max; i++) {
140: s += Integer.toHexString(this .buf[i]) + " "; //$NON-NLS-1$
141: }
142: s = s.substring(0, s.length() - 1) + "\n"; //$NON-NLS-1$
143:
144: return s;
145: }
146:
147: }
|