1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.util.ByteArray |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.util; |
22 | |
23 | import java.io.ObjectOutput; |
24 | import java.io.ObjectInput; |
25 | import java.io.IOException; |
26 | |
27 | /** |
28 | ByteArray wraps java byte arrays (byte[]) to allow |
29 | byte arrays to be used as keys in hashtables. |
30 | |
31 | This is required because the equals function on |
32 | byte[] directly uses reference equality. |
33 | <P> |
34 | This class also allows the trio of array, offset and length |
35 | to be carried around as a single object. |
36 | */ |
37 | public final class ByteArray |
38 | { |
39 | |
40 | private byte[] array; |
41 | private int offset; |
42 | private int length; |
43 | |
44 | /** |
45 | Create an instance of this class that wraps ths given array. |
46 | This class does not make a copy of the array, it just saves |
47 | the reference. |
48 | */ |
49 | public ByteArray(byte[] array, int offset, int length) { |
50 | this.array = array; |
51 | this.offset = offset; |
52 | this.length = length; |
53 | } |
54 | |
55 | public ByteArray(byte[] array) { |
56 | this(array, 0, array.length); |
57 | } |
58 | |
59 | public ByteArray() |
60 | { |
61 | } |
62 | |
63 | public void setBytes(byte[] array) |
64 | { |
65 | this.array = array; |
66 | offset = 0; |
67 | length = array.length; |
68 | } |
69 | |
70 | public void setBytes(byte[] array, int length) |
71 | { |
72 | this.array = array; |
73 | this.offset = 0; |
74 | this.length = length; |
75 | } |
76 | |
77 | public void setBytes(byte[] array, int offset, int length) |
78 | { |
79 | this.array = array; |
80 | this.offset = offset; |
81 | this.length = length; |
82 | } |
83 | |
84 | |
85 | /** |
86 | Value equality for byte arrays. |
87 | */ |
88 | public boolean equals(Object other) { |
89 | if (other instanceof ByteArray) { |
90 | ByteArray ob = (ByteArray) other; |
91 | return ByteArray.equals(array, offset, length, ob.array, ob.offset, ob.length); |
92 | } |
93 | return false; |
94 | } |
95 | |
96 | |
97 | |
98 | /** |
99 | */ |
100 | public int hashCode() { |
101 | |
102 | byte[] larray = array; |
103 | |
104 | int hash = length; |
105 | for (int i = 0; i < length; i++) { |
106 | hash += larray[i + offset]; |
107 | } |
108 | return hash; |
109 | } |
110 | |
111 | public final byte[] getArray() { |
112 | return array; |
113 | } |
114 | public final int getOffset() { |
115 | return offset; |
116 | } |
117 | |
118 | public final int getLength() { |
119 | return length; |
120 | } |
121 | public final void setLength(int newLength) { |
122 | length = newLength; |
123 | } |
124 | |
125 | /** |
126 | * Read this object from a stream of stored objects. |
127 | * |
128 | * @param in read this. |
129 | * |
130 | * @exception IOException thrown on error |
131 | */ |
132 | public void readExternal( ObjectInput in ) throws IOException |
133 | { |
134 | int len = length = in.readInt(); |
135 | offset = 0; |
136 | array = new byte[len]; |
137 | |
138 | in.readFully(array, 0, len); |
139 | } |
140 | |
141 | |
142 | /** |
143 | * Write the byte array out w/o compression |
144 | * |
145 | * @param out write bytes here. |
146 | * |
147 | * @exception IOException thrown on error |
148 | */ |
149 | public void writeExternal(ObjectOutput out) throws IOException |
150 | { |
151 | out.writeInt(length); |
152 | out.write(array, offset, length); |
153 | } |
154 | |
155 | |
156 | |
157 | /** |
158 | Compare two byte arrays using value equality. |
159 | Two byte arrays are equal if their length is |
160 | identical and their contents are identical. |
161 | */ |
162 | private static boolean equals(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) { |
163 | |
164 | if (aLength != bLength) |
165 | return false; |
166 | |
167 | for (int i = 0; i < aLength; i++) { |
168 | if (a[i + aOffset] != b[i + bOffset]) |
169 | return false; |
170 | } |
171 | return true; |
172 | } |
173 | } |
174 | |