1 21 22 package org.apache.derby.iapi.util; 23 24 import java.io.ObjectOutput ; 25 import java.io.ObjectInput ; 26 import java.io.IOException ; 27 28 38 public final class ByteArray 39 { 40 41 private byte[] array; 42 private int offset; 43 private int length; 44 45 50 public ByteArray(byte[] array, int offset, int length) { 51 this.array = array; 52 this.offset = offset; 53 this.length = length; 54 } 55 56 public ByteArray(byte[] array) { 57 this(array, 0, array.length); 58 } 59 60 public ByteArray() 61 { 62 } 63 64 public void setBytes(byte[] array) 65 { 66 this.array = array; 67 offset = 0; 68 length = array.length; 69 } 70 71 public void setBytes(byte[] array, int length) 72 { 73 this.array = array; 74 this.offset = 0; 75 this.length = length; 76 } 77 78 public void setBytes(byte[] array, int offset, int length) 79 { 80 this.array = array; 81 this.offset = offset; 82 this.length = length; 83 } 84 85 86 89 public boolean equals(Object other) { 90 if (other instanceof ByteArray) { 91 ByteArray ob = (ByteArray) other; 92 return ByteArray.equals(array, offset, length, ob.array, ob.offset, ob.length); 93 } 94 return false; 95 } 96 97 98 99 101 public int hashCode() { 102 103 byte[] larray = array; 104 105 int hash = length; 106 for (int i = 0; i < length; i++) { 107 hash += larray[i + offset]; 108 } 109 return hash; 110 } 111 112 public final byte[] getArray() { 113 return array; 114 } 115 public final int getOffset() { 116 return offset; 117 } 118 119 public final int getLength() { 120 return length; 121 } 122 public final void setLength(int newLength) { 123 length = newLength; 124 } 125 126 133 public void readExternal( ObjectInput in ) throws IOException 134 { 135 int len = length = in.readInt(); 136 offset = 0; 137 array = new byte[len]; 138 139 in.readFully(array, 0, len); 140 } 141 142 143 150 public void writeExternal(ObjectOutput out) throws IOException 151 { 152 out.writeInt(length); 153 out.write(array, offset, length); 154 } 155 156 157 158 163 private static boolean equals(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) { 164 165 if (aLength != bLength) 166 return false; 167 168 for (int i = 0; i < aLength; i++) { 169 if (a[i + aOffset] != b[i + bOffset]) 170 return false; 171 } 172 return true; 173 } 174 } 175 176 | Popular Tags |