KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > util > ByteArray


1 /*
2
3    Derby - Class org.apache.derby.iapi.util.ByteArray
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.util;
23
24 import java.io.ObjectOutput JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29     ByteArray wraps java byte arrays (byte[]) to allow
30     byte arrays to be used as keys in hashtables.
31
32     This is required because the equals function on
33     byte[] directly uses reference equality.
34     <P>
35     This class also allows the trio of array, offset and length
36     to be carried around as a single object.
37 */

38 public final class ByteArray
39 {
40
41     private byte[] array;
42     private int offset;
43     private int length;
44
45     /**
46         Create an instance of this class that wraps ths given array.
47         This class does not make a copy of the array, it just saves
48         the reference.
49     */

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     /**
87         Value equality for byte arrays.
88     */

89     public boolean equals(Object JavaDoc 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     /**
100     */

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     /**
127      * Read this object from a stream of stored objects.
128      *
129      * @param in read this.
130      *
131      * @exception IOException thrown on error
132      */

133     public void readExternal( ObjectInput JavaDoc in ) throws IOException JavaDoc
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     /**
144      * Write the byte array out w/o compression
145      *
146      * @param out write bytes here.
147      *
148      * @exception IOException thrown on error
149      */

150     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
151     {
152         out.writeInt(length);
153         out.write(array, offset, length);
154     }
155
156
157
158     /**
159         Compare two byte arrays using value equality.
160         Two byte arrays are equal if their length is
161         identical and their contents are identical.
162     */

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