Path: blob/master/src/java.naming/share/classes/javax/naming/BinaryRefAddr.java
41152 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming;2627/**28* This class represents the binary form of the address of29* a communications end-point.30*<p>31* A BinaryRefAddr consists of a type that describes the communication mechanism32* and an opaque buffer containing the address description33* specific to that communication mechanism. The format and interpretation of34* the address type and the contents of the opaque buffer are based on35* the agreement of three parties: the client that uses the address,36* the object/server that can be reached using the address,37* and the administrator or program that creates the address.38*<p>39* An example of a binary reference address is an BER X.500 presentation address.40* Another example of a binary reference address is a serialized form of41* a service's object handle.42*<p>43* A binary reference address is immutable in the sense that its fields44* once created, cannot be replaced. However, it is possible to access45* the byte array used to hold the opaque buffer. Programs are strongly46* recommended against changing this byte array. Changes to this47* byte array need to be explicitly synchronized.48*49* @author Rosanna Lee50* @author Scott Seligman51*52* @see RefAddr53* @see StringRefAddr54* @since 1.355*/5657/*58* The serialized form of a BinaryRefAddr object consists of its type59* name String and a byte array containing its "contents".60*/6162public class BinaryRefAddr extends RefAddr {63/**64* Contains the bytes of the address.65* This field is initialized by the constructor and returned66* using getAddressBytes() and getAddressContents().67* @serial68*/69private byte[] buf = null;7071/**72* Constructs a new instance of BinaryRefAddr using its address type and a byte73* array for contents.74*75* @param addrType A non-null string describing the type of the address.76* @param src The non-null contents of the address as a byte array.77* The contents of src is copied into the new BinaryRefAddr.78*/79public BinaryRefAddr(String addrType, byte[] src) {80this(addrType, src, 0, src.length);81}8283/**84* Constructs a new instance of BinaryRefAddr using its address type and85* a region of a byte array for contents.86*87* @param addrType A non-null string describing the type of the address.88* @param src The non-null contents of the address as a byte array.89* The contents of src is copied into the new BinaryRefAddr.90* @param offset The starting index in src to get the bytes.91* {@code 0 <= offset <= src.length}.92* @param count The number of bytes to extract from src.93* {@code 0 <= count <= src.length-offset}.94*/95public BinaryRefAddr(String addrType, byte[] src, int offset, int count) {96super(addrType);97buf = new byte[count];98System.arraycopy(src, offset, buf, 0, count);99}100101/**102* Retrieves the contents of this address as an Object.103* The result is a byte array.104* Changes to this array will affect this BinaryRefAddr's contents.105* Programs are recommended against changing this array's contents106* and to lock the buffer if they need to change it.107*108* @return The non-null buffer containing this address's contents.109*/110public Object getContent() {111return buf;112}113114115/**116* Determines whether obj is equal to this address. It is equal if117* it contains the same address type and their contents are byte-wise118* equivalent.119* @param obj The possibly null object to check.120* @return true if the object is equal; false otherwise.121*/122public boolean equals(Object obj) {123if ((obj != null) && (obj instanceof BinaryRefAddr)) {124BinaryRefAddr target = (BinaryRefAddr)obj;125if (addrType.compareTo(target.addrType) == 0) {126if (buf == null && target.buf == null)127return true;128if (buf == null || target.buf == null ||129buf.length != target.buf.length)130return false;131for (int i = 0; i < buf.length; i++)132if (buf[i] != target.buf[i])133return false;134return true;135}136}137return false;138}139140/**141* Computes the hash code of this address using its address type and contents.142* Two BinaryRefAddrs have the same hash code if they have143* the same address type and the same contents.144* It is also possible for different BinaryRefAddrs to have145* the same hash code.146*147* @return The hash code of this address as an int.148*/149public int hashCode() {150int hash = addrType.hashCode();151for (int i = 0; i < buf.length; i++) {152hash += buf[i]; // %%% improve later153}154return hash;155}156157/**158* Generates the string representation of this address.159* The string consists of the address's type and contents with labels.160* The first 32 bytes of contents are displayed (in hexadecimal).161* If there are more than 32 bytes, "..." is used to indicate more.162* This string is meant to used for debugging purposes and not163* meant to be interpreted programmatically.164* @return The non-null string representation of this address.165*/166public String toString(){167StringBuilder str = new StringBuilder("Address Type: " + addrType + "\n");168169str.append("AddressContents: ");170for (int i = 0; i<buf.length && i < 32; i++) {171str.append(Integer.toHexString(buf[i]) +" ");172}173if (buf.length >= 32)174str.append(" ...\n");175return (str.toString());176}177178/**179* Use serialVersionUID from JNDI 1.1.1 for interoperability180*/181private static final long serialVersionUID = -3415254970957330361L;182}183184185