| 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 ([email protected])
|
|---|
| 50 | */
|
|---|
| 51 | public class BinaryRefAddr extends RefAddr
|
|---|
| 52 | {
|
|---|
| 53 | static final long serialVersionUID = -3415254970957330361L;
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * The possibly null content of this RefAddr.
|
|---|
| 57 | * Set by the constructor and returned by getContent.
|
|---|
| 58 | */
|
|---|
| 59 | private final byte[] buf;
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Contructs a new BinaryRefAddr with the given type and content.
|
|---|
| 63 | * The complete content of the byte array is copied to a new array.
|
|---|
| 64 | */
|
|---|
| 65 | public BinaryRefAddr (String addrType, byte[] buf)
|
|---|
| 66 | {
|
|---|
| 67 | this(addrType, buf, 0, buf.length);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Contructs a new BinaryRefAddr with the given type and the content
|
|---|
| 72 | * taken from the given byte array.
|
|---|
| 73 | * The content of the byte array is copied to a new array.
|
|---|
| 74 | */
|
|---|
| 75 | public BinaryRefAddr (String addrType, byte[] buf, int off, int length)
|
|---|
| 76 | {
|
|---|
| 77 | super(addrType);
|
|---|
| 78 | this.buf = new byte[length];
|
|---|
| 79 | System.arraycopy(buf, off, this.buf, 0, length);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Returns the byte array contents as given to the constructor.
|
|---|
| 84 | * The returned byte array is shared with this object and other callers.
|
|---|
| 85 | * Changing the content of the buffer is discouraged and should only be
|
|---|
| 86 | * done when the byte array is locked.
|
|---|
| 87 | */
|
|---|
| 88 | public Object getContent ()
|
|---|
| 89 | {
|
|---|
| 90 | return buf;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Checks if the object is a BinaryRefAddr with the same type and with the
|
|---|
| 95 | * same bytes in the content.
|
|---|
| 96 | *
|
|---|
| 97 | * @return true if the given object is an instance of BinaryRefAddr,
|
|---|
| 98 | * the addrType is the same as this addrType and the bytes of the
|
|---|
| 99 | * 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 | }
|
|---|