1 package org.apache.java.lang;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 /***
58 * Static methods for managing byte arrays (all methods follow Big
59 * Endian order where most significant bits are in front).
60 *
61 * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
62 * @version $Id: Bytes.java,v 1.1.1.1 2001/08/16 05:08:26 jvanzyl Exp $
63 */
64 public class Bytes
65 {
66 private static final char[] hexDigits =
67 {
68 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
69 };
70
71 /***
72 * Appends two bytes array into one.
73 *
74 * @param a A byte[].
75 * @param b A byte[].
76 * @return A byte[].
77 */
78 public static byte[] append(byte[] a,
79 byte[] b)
80 {
81 byte[] z = new byte[a.length + b.length];
82 System.arraycopy(a, 0, z, 0, a.length);
83 System.arraycopy(b, 0, z, a.length, b.length);
84 return z;
85 }
86
87 /***
88 * Appends three bytes array into one.
89 *
90 * @param a A byte[].
91 * @param b A byte[].
92 * @param c A byte[].
93 * @return A byte[].
94 */
95 public static byte[] append(byte[] a,
96 byte[] b,
97 byte[] c)
98 {
99 byte[] z = new byte[a.length + b.length + c.length];
100 System.arraycopy(a, 0, z, 0, a.length);
101 System.arraycopy(b, 0, z, a.length, b.length);
102 System.arraycopy(c, 0, z, a.length + b.length, c.length);
103 return z;
104 }
105
106 /***
107 * Compares two byte arrays for equality.
108 *
109 * @param a A byte[].
110 * @param b A byte[].
111 * @return True if the arrays have identical contents.
112 */
113 public static boolean areEqual(byte[] a,
114 byte[] b)
115 {
116 int aLength = a.length;
117 if (aLength != b.length) return false;
118
119 for (int i = 0; i < aLength; i++)
120 if (a[i] != b[i])
121 return false;
122
123 return true;
124 }
125
126 /***
127 * Gets the end of the byte array given.
128 *
129 * @param b A byte[].
130 * @param pos The position from which to start.
131 * @return A byte[] consisting of the portion of b between pos and
132 * the end of b.
133 */
134 public static byte[] copy(byte[] b,
135 int pos)
136 {
137 return copy(b, pos, b.length - pos);
138 }
139
140 /***
141 * Gets a sub-set of the byte array given.
142 *
143 * @param b A byte[].
144 * @param pos The position from which to start.
145 * @param length The number of bytes to copy from the original
146 * byte array to the new one.
147 * @return A byte[] consisting of the portion of b starting at pos
148 * and continuing for length bytes, or until the end of b is
149 * reached, which ever occurs first.
150 */
151 public static byte[] copy(byte[] b,
152 int pos,
153 int length)
154 {
155 byte[] z = new byte[length];
156 System.arraycopy(b, pos, z, 0, length);
157 return z;
158 }
159
160 /***
161 * Merges a bytes array into another.
162 *
163 * @param src A byte[].
164 * @param dest A byte[].
165 */
166 public static void merge(byte[] src,
167 byte[] dest)
168 {
169 System.arraycopy(src, 0, dest, 0, src.length);
170 }
171
172 /***
173 * Merges a bytes array into another starting from the
174 * given position.
175 *
176 * @param src A byte[].
177 * @param dest A byte[].
178 * @param pos The position from which to start.
179 */
180 public static void merge(byte[] src,
181 byte[] dest,
182 int pos)
183 {
184 System.arraycopy(src, 0, dest, pos, src.length);
185 }
186
187 /***
188 * Merges a bytes array into another starting from the
189 * given position.
190 *
191 * @param src A byte[].
192 * @param dest A byte[].
193 * @param pos The position from which to start.
194 * @param length The number of bytes to merge.
195 */
196 public static void merge(byte[] src,
197 byte[] dest,
198 int pos,
199 int length)
200 {
201 System.arraycopy(src, 0, dest, pos, length);
202 }
203
204 /***
205 * Merges a bytes array into another starting from the
206 * given positions.
207 *
208 * @param src A byte[].
209 * @param dest A byte[].
210 * @param srcpos The position from which to start in src.
211 * @param destpos The position from which to start in dest.
212 * @param length The number of bytes to merge.
213 */
214 public static void merge(byte[] src,
215 byte[] dest,
216 int srcpos,
217 int destpos,
218 int length)
219 {
220 System.arraycopy(src, srcpos, dest, destpos, length);
221 }
222
223 /***
224 * Returns a 4-byte array built from an int.
225 *
226 * @param n The number to convert.
227 * @return A byte[].
228 */
229 public static byte[] toBytes(int n)
230 {
231 return toBytes(n, new byte[4]);
232 }
233
234 /***
235 * Build a 4-byte array from an int. No check is performed on the
236 * array length.
237 *
238 * @param n The number to convert.
239 * @param b The array to fill.
240 * @return A byte[].
241 */
242 public static byte[] toBytes(int n,
243 byte[] b)
244 {
245 b[3] = (byte) (n);
246 n >>>= 8;
247 b[2] = (byte) (n);
248 n >>>= 8;
249 b[1] = (byte) (n);
250 n >>>= 8;
251 b[0] = (byte) (n);
252
253 return b;
254 }
255
256 /***
257 * Returns a 8-byte array built from a long.
258 *
259 * @param n The number to convert.
260 * @return A byte[].
261 */
262 public static byte[] toBytes(long n)
263 {
264 return toBytes(n, new byte[8]);
265 }
266
267 /***
268 * Build a 8-byte array from a long. No check is performed on the
269 * array length.
270 *
271 * @param n The number to convert.
272 * @param b The array to fill.
273 * @return A byte[].
274 */
275 public static byte[] toBytes(long n,
276 byte[] b)
277 {
278 b[7] = (byte) (n);
279 n >>>= 8;
280 b[6] = (byte) (n);
281 n >>>= 8;
282 b[5] = (byte) (n);
283 n >>>= 8;
284 b[4] = (byte) (n);
285 n >>>= 8;
286 b[3] = (byte) (n);
287 n >>>= 8;
288 b[2] = (byte) (n);
289 n >>>= 8;
290 b[1] = (byte) (n);
291 n >>>= 8;
292 b[0] = (byte) (n);
293
294 return b;
295 }
296
297 /***
298 * Build an int from first 4 bytes of the array.
299 *
300 * @param b The byte[] to convert.
301 * @return An int.
302 */
303 public static int toInt(byte[] b)
304 {
305 return ((((int) b[3]) & 0xFF) +
306 ((((int) b[2]) & 0xFF) << 8) +
307 ((((int) b[1]) & 0xFF) << 16) +
308 ((((int) b[0]) & 0xFF) << 24));
309 }
310
311 /***
312 * Build a long from first 8 bytes of the array.
313 *
314 * @param b The byte[] to convert.
315 * @return A long.
316 */
317 public static long toLong(byte[] b)
318 {
319 return ((((long) b[7]) & 0xFF) +
320 ((((long) b[6]) & 0xFF) << 8) +
321 ((((long) b[5]) & 0xFF) << 16) +
322 ((((long) b[4]) & 0xFF) << 24) +
323 ((((long) b[3]) & 0xFF) << 32) +
324 ((((long) b[2]) & 0xFF) << 40) +
325 ((((long) b[1]) & 0xFF) << 48) +
326 ((((long) b[0]) & 0xFF) << 56));
327 }
328
329 /***
330 * Returns a string of hexadecimal digits from a byte array.
331 *
332 * @param b The byte[] to convert.
333 * @return A String.
334 */
335 public static String toString(byte[] b)
336 {
337 return toString(b, 0, b.length);
338 }
339
340 /***
341 * Returns a string of hexadecimal digits from a byte array,
342 * starting at offset and continuing for length bytes.
343 *
344 * @param b The byte[] to convert.
345 * @param offset An int.
346 * @param length An int.
347 * @return A String.
348 */
349 public static String toString(byte[] b,
350 int offset,
351 int length)
352 {
353 char[] buf = new char[length * 2];
354
355 for (int i = offset, j = 0, k; i < offset + length; i++)
356 {
357 k = b[i];
358 buf[j++] = hexDigits[(k >>> 4) & 0x0F];
359 buf[j++] = hexDigits[k & 0x0F];
360 }
361
362 return new String(buf);
363 }
364 }
This page was automatically generated by Maven