001: package org.apache.java.lang;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * Static methods for managing byte arrays (all methods follow Big
021: * Endian order where most significant bits are in front).
022: *
023: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
024: * @version $Id: Bytes.java 264148 2005-08-29 14:21:04Z henning $
025: * @deprecated Use Jakarta Commons
026: */
027: public class Bytes {
028: private static final char[] hexDigits = { '0', '1', '2', '3', '4',
029: '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
030:
031: /**
032: * Appends two bytes array into one.
033: *
034: * @param a A byte[].
035: * @param b A byte[].
036: * @return A byte[].
037: */
038: public static byte[] append(byte[] a, byte[] b) {
039: byte[] z = new byte[a.length + b.length];
040: System.arraycopy(a, 0, z, 0, a.length);
041: System.arraycopy(b, 0, z, a.length, b.length);
042: return z;
043: }
044:
045: /**
046: * Appends three bytes array into one.
047: *
048: * @param a A byte[].
049: * @param b A byte[].
050: * @param c A byte[].
051: * @return A byte[].
052: */
053: public static byte[] append(byte[] a, byte[] b, byte[] c) {
054: byte[] z = new byte[a.length + b.length + c.length];
055: System.arraycopy(a, 0, z, 0, a.length);
056: System.arraycopy(b, 0, z, a.length, b.length);
057: System.arraycopy(c, 0, z, a.length + b.length, c.length);
058: return z;
059: }
060:
061: /**
062: * Compares two byte arrays for equality.
063: *
064: * @param a A byte[].
065: * @param b A byte[].
066: * @return True if the arrays have identical contents.
067: */
068: public static boolean areEqual(byte[] a, byte[] b) {
069: int aLength = a.length;
070: if (aLength != b.length)
071: return false;
072:
073: for (int i = 0; i < aLength; i++)
074: if (a[i] != b[i])
075: return false;
076:
077: return true;
078: }
079:
080: /**
081: * Gets the end of the byte array given.
082: *
083: * @param b A byte[].
084: * @param pos The position from which to start.
085: * @return A byte[] consisting of the portion of b between pos and
086: * the end of b.
087: */
088: public static byte[] copy(byte[] b, int pos) {
089: return copy(b, pos, b.length - pos);
090: }
091:
092: /**
093: * Gets a sub-set of the byte array given.
094: *
095: * @param b A byte[].
096: * @param pos The position from which to start.
097: * @param length The number of bytes to copy from the original
098: * byte array to the new one.
099: * @return A byte[] consisting of the portion of b starting at pos
100: * and continuing for length bytes, or until the end of b is
101: * reached, which ever occurs first.
102: */
103: public static byte[] copy(byte[] b, int pos, int length) {
104: byte[] z = new byte[length];
105: System.arraycopy(b, pos, z, 0, length);
106: return z;
107: }
108:
109: /**
110: * Merges a bytes array into another.
111: *
112: * @param src A byte[].
113: * @param dest A byte[].
114: */
115: public static void merge(byte[] src, byte[] dest) {
116: System.arraycopy(src, 0, dest, 0, src.length);
117: }
118:
119: /**
120: * Merges a bytes array into another starting from the
121: * given position.
122: *
123: * @param src A byte[].
124: * @param dest A byte[].
125: * @param pos The position from which to start.
126: */
127: public static void merge(byte[] src, byte[] dest, int pos) {
128: System.arraycopy(src, 0, dest, pos, src.length);
129: }
130:
131: /**
132: * Merges a bytes array into another starting from the
133: * given position.
134: *
135: * @param src A byte[].
136: * @param dest A byte[].
137: * @param pos The position from which to start.
138: * @param length The number of bytes to merge.
139: */
140: public static void merge(byte[] src, byte[] dest, int pos,
141: int length) {
142: System.arraycopy(src, 0, dest, pos, length);
143: }
144:
145: /**
146: * Merges a bytes array into another starting from the
147: * given positions.
148: *
149: * @param src A byte[].
150: * @param dest A byte[].
151: * @param srcpos The position from which to start in src.
152: * @param destpos The position from which to start in dest.
153: * @param length The number of bytes to merge.
154: */
155: public static void merge(byte[] src, byte[] dest, int srcpos,
156: int destpos, int length) {
157: System.arraycopy(src, srcpos, dest, destpos, length);
158: }
159:
160: /**
161: * Returns a 4-byte array built from an int.
162: *
163: * @param n The number to convert.
164: * @return A byte[].
165: */
166: public static byte[] toBytes(int n) {
167: return toBytes(n, new byte[4]);
168: }
169:
170: /**
171: * Build a 4-byte array from an int. No check is performed on the
172: * array length.
173: *
174: * @param n The number to convert.
175: * @param b The array to fill.
176: * @return A byte[].
177: */
178: public static byte[] toBytes(int n, byte[] b) {
179: b[3] = (byte) (n);
180: n >>>= 8;
181: b[2] = (byte) (n);
182: n >>>= 8;
183: b[1] = (byte) (n);
184: n >>>= 8;
185: b[0] = (byte) (n);
186:
187: return b;
188: }
189:
190: /**
191: * Returns a 8-byte array built from a long.
192: *
193: * @param n The number to convert.
194: * @return A byte[].
195: */
196: public static byte[] toBytes(long n) {
197: return toBytes(n, new byte[8]);
198: }
199:
200: /**
201: * Build a 8-byte array from a long. No check is performed on the
202: * array length.
203: *
204: * @param n The number to convert.
205: * @param b The array to fill.
206: * @return A byte[].
207: */
208: public static byte[] toBytes(long n, byte[] b) {
209: b[7] = (byte) (n);
210: n >>>= 8;
211: b[6] = (byte) (n);
212: n >>>= 8;
213: b[5] = (byte) (n);
214: n >>>= 8;
215: b[4] = (byte) (n);
216: n >>>= 8;
217: b[3] = (byte) (n);
218: n >>>= 8;
219: b[2] = (byte) (n);
220: n >>>= 8;
221: b[1] = (byte) (n);
222: n >>>= 8;
223: b[0] = (byte) (n);
224:
225: return b;
226: }
227:
228: /**
229: * Build an int from first 4 bytes of the array.
230: *
231: * @param b The byte[] to convert.
232: * @return An int.
233: */
234: public static int toInt(byte[] b) {
235: return ((((int) b[3]) & 0xFF) + ((((int) b[2]) & 0xFF) << 8)
236: + ((((int) b[1]) & 0xFF) << 16) + ((((int) b[0]) & 0xFF) << 24));
237: }
238:
239: /**
240: * Build a long from first 8 bytes of the array.
241: *
242: * @param b The byte[] to convert.
243: * @return A long.
244: */
245: public static long toLong(byte[] b) {
246: return ((((long) b[7]) & 0xFF) + ((((long) b[6]) & 0xFF) << 8)
247: + ((((long) b[5]) & 0xFF) << 16)
248: + ((((long) b[4]) & 0xFF) << 24)
249: + ((((long) b[3]) & 0xFF) << 32)
250: + ((((long) b[2]) & 0xFF) << 40)
251: + ((((long) b[1]) & 0xFF) << 48) + ((((long) b[0]) & 0xFF) << 56));
252: }
253:
254: /**
255: * Returns a string of hexadecimal digits from a byte array.
256: *
257: * @param b The byte[] to convert.
258: * @return A String.
259: */
260: public static String toString(byte[] b) {
261: return toString(b, 0, b.length);
262: }
263:
264: /**
265: * Returns a string of hexadecimal digits from a byte array,
266: * starting at offset and continuing for length bytes.
267: *
268: * @param b The byte[] to convert.
269: * @param offset An int.
270: * @param length An int.
271: * @return A String.
272: */
273: public static String toString(byte[] b, int offset, int length) {
274: char[] buf = new char[length * 2];
275:
276: for (int i = offset, j = 0, k; i < offset + length; i++) {
277: k = b[i];
278: buf[j++] = hexDigits[(k >>> 4) & 0x0F];
279: buf[j++] = hexDigits[k & 0x0F];
280: }
281:
282: return new String(buf);
283: }
284: }
|