KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > datatypes > ByteArray


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

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 JavaDoc 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 JavaDoc 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                 // pass through
84
case 2:
85                 tmp |= data[4*len+1] << 16;
86                 // pass through
87
case 1:
88                 tmp |= data[4*len] << 24;
89                 // pass through
90
default:
91                 result ^= tmp;
92         }
93         return result;
94     }
95 }
96
Popular Tags