%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.java.lang.Bytes |
|
|
1 | package org.apache.java.lang; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | /** |
|
20 | * Static methods for managing byte arrays (all methods follow Big |
|
21 | * Endian order where most significant bits are in front). |
|
22 | * |
|
23 | * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a> |
|
24 | * @version $Id: Bytes.java 264148 2005-08-29 14:21:04Z henning $ |
|
25 | * @deprecated Use Jakarta Commons |
|
26 | */ |
|
27 | 0 | public class Bytes |
28 | { |
|
29 | 0 | private static final char[] hexDigits = |
30 | { |
|
31 | '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' |
|
32 | }; |
|
33 | ||
34 | /** |
|
35 | * Appends two bytes array into one. |
|
36 | * |
|
37 | * @param a A byte[]. |
|
38 | * @param b A byte[]. |
|
39 | * @return A byte[]. |
|
40 | */ |
|
41 | public static byte[] append(byte[] a, |
|
42 | byte[] b) |
|
43 | { |
|
44 | 0 | byte[] z = new byte[a.length + b.length]; |
45 | 0 | System.arraycopy(a, 0, z, 0, a.length); |
46 | 0 | System.arraycopy(b, 0, z, a.length, b.length); |
47 | 0 | return z; |
48 | } |
|
49 | ||
50 | /** |
|
51 | * Appends three bytes array into one. |
|
52 | * |
|
53 | * @param a A byte[]. |
|
54 | * @param b A byte[]. |
|
55 | * @param c A byte[]. |
|
56 | * @return A byte[]. |
|
57 | */ |
|
58 | public static byte[] append(byte[] a, |
|
59 | byte[] b, |
|
60 | byte[] c) |
|
61 | { |
|
62 | 0 | byte[] z = new byte[a.length + b.length + c.length]; |
63 | 0 | System.arraycopy(a, 0, z, 0, a.length); |
64 | 0 | System.arraycopy(b, 0, z, a.length, b.length); |
65 | 0 | System.arraycopy(c, 0, z, a.length + b.length, c.length); |
66 | 0 | return z; |
67 | } |
|
68 | ||
69 | /** |
|
70 | * Compares two byte arrays for equality. |
|
71 | * |
|
72 | * @param a A byte[]. |
|
73 | * @param b A byte[]. |
|
74 | * @return True if the arrays have identical contents. |
|
75 | */ |
|
76 | public static boolean areEqual(byte[] a, |
|
77 | byte[] b) |
|
78 | { |
|
79 | 0 | int aLength = a.length; |
80 | 0 | if (aLength != b.length) return false; |
81 | ||
82 | 0 | for (int i = 0; i < aLength; i++) |
83 | 0 | if (a[i] != b[i]) |
84 | 0 | return false; |
85 | ||
86 | 0 | return true; |
87 | } |
|
88 | ||
89 | /** |
|
90 | * Gets the end of the byte array given. |
|
91 | * |
|
92 | * @param b A byte[]. |
|
93 | * @param pos The position from which to start. |
|
94 | * @return A byte[] consisting of the portion of b between pos and |
|
95 | * the end of b. |
|
96 | */ |
|
97 | public static byte[] copy(byte[] b, |
|
98 | int pos) |
|
99 | { |
|
100 | 0 | return copy(b, pos, b.length - pos); |
101 | } |
|
102 | ||
103 | /** |
|
104 | * Gets a sub-set of the byte array given. |
|
105 | * |
|
106 | * @param b A byte[]. |
|
107 | * @param pos The position from which to start. |
|
108 | * @param length The number of bytes to copy from the original |
|
109 | * byte array to the new one. |
|
110 | * @return A byte[] consisting of the portion of b starting at pos |
|
111 | * and continuing for length bytes, or until the end of b is |
|
112 | * reached, which ever occurs first. |
|
113 | */ |
|
114 | public static byte[] copy(byte[] b, |
|
115 | int pos, |
|
116 | int length) |
|
117 | { |
|
118 | 0 | byte[] z = new byte[length]; |
119 | 0 | System.arraycopy(b, pos, z, 0, length); |
120 | 0 | return z; |
121 | } |
|
122 | ||
123 | /** |
|
124 | * Merges a bytes array into another. |
|
125 | * |
|
126 | * @param src A byte[]. |
|
127 | * @param dest A byte[]. |
|
128 | */ |
|
129 | public static void merge(byte[] src, |
|
130 | byte[] dest) |
|
131 | { |
|
132 | 0 | System.arraycopy(src, 0, dest, 0, src.length); |
133 | 0 | } |
134 | ||
135 | /** |
|
136 | * Merges a bytes array into another starting from the |
|
137 | * given position. |
|
138 | * |
|
139 | * @param src A byte[]. |
|
140 | * @param dest A byte[]. |
|
141 | * @param pos The position from which to start. |
|
142 | */ |
|
143 | public static void merge(byte[] src, |
|
144 | byte[] dest, |
|
145 | int pos) |
|
146 | { |
|
147 | 0 | System.arraycopy(src, 0, dest, pos, src.length); |
148 | 0 | } |
149 | ||
150 | /** |
|
151 | * Merges a bytes array into another starting from the |
|
152 | * given position. |
|
153 | * |
|
154 | * @param src A byte[]. |
|
155 | * @param dest A byte[]. |
|
156 | * @param pos The position from which to start. |
|
157 | * @param length The number of bytes to merge. |
|
158 | */ |
|
159 | public static void merge(byte[] src, |
|
160 | byte[] dest, |
|
161 | int pos, |
|
162 | int length) |
|
163 | { |
|
164 | 0 | System.arraycopy(src, 0, dest, pos, length); |
165 | 0 | } |
166 | ||
167 | /** |
|
168 | * Merges a bytes array into another starting from the |
|
169 | * given positions. |
|
170 | * |
|
171 | * @param src A byte[]. |
|
172 | * @param dest A byte[]. |
|
173 | * @param srcpos The position from which to start in src. |
|
174 | * @param destpos The position from which to start in dest. |
|
175 | * @param length The number of bytes to merge. |
|
176 | */ |
|
177 | public static void merge(byte[] src, |
|
178 | byte[] dest, |
|
179 | int srcpos, |
|
180 | int destpos, |
|
181 | int length) |
|
182 | { |
|
183 | 0 | System.arraycopy(src, srcpos, dest, destpos, length); |
184 | 0 | } |
185 | ||
186 | /** |
|
187 | * Returns a 4-byte array built from an int. |
|
188 | * |
|
189 | * @param n The number to convert. |
|
190 | * @return A byte[]. |
|
191 | */ |
|
192 | public static byte[] toBytes(int n) |
|
193 | { |
|
194 | 0 | return toBytes(n, new byte[4]); |
195 | } |
|
196 | ||
197 | /** |
|
198 | * Build a 4-byte array from an int. No check is performed on the |
|
199 | * array length. |
|
200 | * |
|
201 | * @param n The number to convert. |
|
202 | * @param b The array to fill. |
|
203 | * @return A byte[]. |
|
204 | */ |
|
205 | public static byte[] toBytes(int n, |
|
206 | byte[] b) |
|
207 | { |
|
208 | 0 | b[3] = (byte) (n); |
209 | 0 | n >>>= 8; |
210 | 0 | b[2] = (byte) (n); |
211 | 0 | n >>>= 8; |
212 | 0 | b[1] = (byte) (n); |
213 | 0 | n >>>= 8; |
214 | 0 | b[0] = (byte) (n); |
215 | ||
216 | 0 | return b; |
217 | } |
|
218 | ||
219 | /** |
|
220 | * Returns a 8-byte array built from a long. |
|
221 | * |
|
222 | * @param n The number to convert. |
|
223 | * @return A byte[]. |
|
224 | */ |
|
225 | public static byte[] toBytes(long n) |
|
226 | { |
|
227 | 0 | return toBytes(n, new byte[8]); |
228 | } |
|
229 | ||
230 | /** |
|
231 | * Build a 8-byte array from a long. No check is performed on the |
|
232 | * array length. |
|
233 | * |
|
234 | * @param n The number to convert. |
|
235 | * @param b The array to fill. |
|
236 | * @return A byte[]. |
|
237 | */ |
|
238 | public static byte[] toBytes(long n, |
|
239 | byte[] b) |
|
240 | { |
|
241 | 0 | b[7] = (byte) (n); |
242 | 0 | n >>>= 8; |
243 | 0 | b[6] = (byte) (n); |
244 | 0 | n >>>= 8; |
245 | 0 | b[5] = (byte) (n); |
246 | 0 | n >>>= 8; |
247 | 0 | b[4] = (byte) (n); |
248 | 0 | n >>>= 8; |
249 | 0 | b[3] = (byte) (n); |
250 | 0 | n >>>= 8; |
251 | 0 | b[2] = (byte) (n); |
252 | 0 | n >>>= 8; |
253 | 0 | b[1] = (byte) (n); |
254 | 0 | n >>>= 8; |
255 | 0 | b[0] = (byte) (n); |
256 | ||
257 | 0 | return b; |
258 | } |
|
259 | ||
260 | /** |
|
261 | * Build an int from first 4 bytes of the array. |
|
262 | * |
|
263 | * @param b The byte[] to convert. |
|
264 | * @return An int. |
|
265 | */ |
|
266 | public static int toInt(byte[] b) |
|
267 | { |
|
268 | 0 | return ((((int) b[3]) & 0xFF) + |
269 | ((((int) b[2]) & 0xFF) << 8) + |
|
270 | ((((int) b[1]) & 0xFF) << 16) + |
|
271 | ((((int) b[0]) & 0xFF) << 24)); |
|
272 | } |
|
273 | ||
274 | /** |
|
275 | * Build a long from first 8 bytes of the array. |
|
276 | * |
|
277 | * @param b The byte[] to convert. |
|
278 | * @return A long. |
|
279 | */ |
|
280 | public static long toLong(byte[] b) |
|
281 | { |
|
282 | 0 | return ((((long) b[7]) & 0xFF) + |
283 | ((((long) b[6]) & 0xFF) << 8) + |
|
284 | ((((long) b[5]) & 0xFF) << 16) + |
|
285 | ((((long) b[4]) & 0xFF) << 24) + |
|
286 | ((((long) b[3]) & 0xFF) << 32) + |
|
287 | ((((long) b[2]) & 0xFF) << 40) + |
|
288 | ((((long) b[1]) & 0xFF) << 48) + |
|
289 | ((((long) b[0]) & 0xFF) << 56)); |
|
290 | } |
|
291 | ||
292 | /** |
|
293 | * Returns a string of hexadecimal digits from a byte array. |
|
294 | * |
|
295 | * @param b The byte[] to convert. |
|
296 | * @return A String. |
|
297 | */ |
|
298 | public static String toString(byte[] b) |
|
299 | { |
|
300 | 0 | return toString(b, 0, b.length); |
301 | } |
|
302 | ||
303 | /** |
|
304 | * Returns a string of hexadecimal digits from a byte array, |
|
305 | * starting at offset and continuing for length bytes. |
|
306 | * |
|
307 | * @param b The byte[] to convert. |
|
308 | * @param offset An int. |
|
309 | * @param length An int. |
|
310 | * @return A String. |
|
311 | */ |
|
312 | public static String toString(byte[] b, |
|
313 | int offset, |
|
314 | int length) |
|
315 | { |
|
316 | 0 | char[] buf = new class="keyword">char[length * 2]; |
317 | ||
318 | 0 | for (int i = offset, j = 0, k; i < offset + length; i++) |
319 | { |
|
320 | 0 | k = b[i]; |
321 | 0 | buf[j++] = hexDigits[(k >>> 4) & 0x0F]; |
322 | 0 | buf[j++] = hexDigits[k & 0x0F]; |
323 | } |
|
324 | ||
325 | 0 | return new String(buf); |
326 | } |
|
327 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |