1 22 23 package org.xquark.schema.datatypes; 24 25 26 public class ByteArray { 27 28 private byte[] data; 29 private int hashCode; 30 31 public ByteArray(byte[] data) { 32 this.data = data; 33 this.hashCode = computeHashCode(data); 34 } 35 36 public ByteArray(byte[] data, int hashCode) { 37 this.data = data; 38 this.hashCode = hashCode; 39 } 40 41 public byte[] getData() { 42 return data; 43 } 44 45 public int getLength() { 46 return data.length; 47 } 48 49 public boolean equals(Object obj) { 50 if (obj == this) return true; 51 if (!checkClass(obj)) return false; 52 ByteArray other = (ByteArray)obj; 53 int len = getLength(); 54 if (other.getLength() != len) return false; 55 for (int i = 0; i < len; i++) 56 if (data[i] != other.data[i]) return false; 57 return true; 58 } 59 60 protected boolean checkClass(Object obj) { 61 return obj instanceof ByteArray; 62 } 63 64 public int hashCode() { 65 return hashCode; 66 } 67 68 public static int computeHashCode(byte[] data) { 69 int len = data.length/4; 70 int result = 0; 71 int tmp = 0; 72 for (int i = 0; i < len; i++) { 73 tmp = data[4*i] << 24; 74 tmp |= data[4*i+1] << 16; 75 tmp |= data[4*i+2] << 8; 76 tmp |= data[4*i+3]; 77 result ^= tmp; 78 } 79 tmp = 0; 80 switch (data.length%4) { 81 case 3: 82 tmp |= data[4*len+2] << 8; 83 case 2: 85 tmp |= data[4*len+1] << 16; 86 case 1: 88 tmp |= data[4*len] << 24; 89 default: 91 result ^= tmp; 92 } 93 return result; 94 } 95 } 96 | Popular Tags |