2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.util;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedInteger;
12 import io.netty.buffer.ByteBuf;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.nio.ByteBuffer;
17 import java.nio.charset.CharacterCodingException;
18 import java.nio.charset.Charset;
19 import java.util.Arrays;
20 import java.util.BitSet;
21 import org.apache.commons.codec.binary.Hex;
24 * Util class for methods working with byte array.
26 public final class ByteArray {
31 * Helper method missing from netty ByteBuf methods. Directly returns byte array part of the given buffer, starting
32 * at reader index, with given length. Increases reader index of the buffer by 'length'.
34 * @param buffer ByteBuf from which the bytes are going to be taken
35 * @param length length of the returned byte array
38 public static byte[] readBytes(final ByteBuf buffer, final int length) {
39 Preconditions.checkArgument(buffer != null && buffer.readableBytes() >= length,
40 "Buffer cannot be read for %s bytes.", length);
41 final byte[] result = new byte[length];
42 buffer.readBytes(result);
47 * Helper method missing from netty ByteBuf methods. Directly returns all readable bytes from buffer as byte array.
48 * Adjusts reader index of the buffer by length of readable bytes in the buffer.
50 * @param buffer byteBuf from which the bytes are going to be taken
53 public static byte[] readAllBytes(final ByteBuf buffer) {
54 return readBytes(buffer, buffer.readableBytes());
58 * Helper method missing from netty ByteBuf methods. Directly returns byte array part of the given buffer, starting
59 * at reader index, with given length. Does not modify reader or writer index of the buffer.
61 * @param buffer ByteBuf from which the bytes are going to be taken
62 * @param length length of the returned byte array
65 public static byte[] getBytes(final ByteBuf buffer, final int length) {
66 Preconditions.checkArgument(buffer != null && buffer.readableBytes() >= length,
67 "Buffer cannot be read for %s bytes.", length);
68 final byte[] result = new byte[length];
69 buffer.getBytes(buffer.readerIndex(), result);
74 * Helper method missing from netty ByteBuf methods. Directly returns all readable bytes from buffer as byte array.
75 * Does not modify writer or reader index of the buffer.
77 * @param buffer byteBuf from which the bytes are going to be taken
80 public static byte[] getAllBytes(final ByteBuf buffer) {
81 return getBytes(buffer, buffer.readableBytes());
85 * Returns a new byte array from given byte array, starting at start index with the size of the length parameter.
86 * Byte array given as parameter stays untouched.
88 * @param bytes original byte array
89 * @param startIndex beginning index, inclusive
90 * @param length how many bytes should be in the sub-array
91 * @return a new byte array that is a sub-array of the original
93 public static byte[] subByte(final byte[] bytes, final int startIndex, final int length) {
94 if (bytes.length == 0 || length < 0 || length > bytes.length || startIndex < 0 || startIndex > bytes.length
95 || startIndex + length > bytes.length) {
96 throw new IllegalArgumentException("Cannot create subByte, invalid arguments: Length: " + length + " startIndex: " + startIndex);
98 final byte[] res = new byte[length];
99 System.arraycopy(bytes, startIndex, res, 0, length);
104 * Converts byte array to Integer. If there are less bytes in the array as required (4), the method will push
105 * adequate number of zero bytes prepending given byte array.
107 * @param bytes array to be converted to int
110 public static int bytesToInt(final byte[] bytes) {
111 if (bytes.length > Integer.SIZE / Byte.SIZE) {
112 throw new IllegalArgumentException("Cannot convert bytes to integer. Byte array too big.");
114 byte[] res = new byte[Integer.SIZE / Byte.SIZE];
115 if (bytes.length != Integer.SIZE / Byte.SIZE) {
116 System.arraycopy(bytes, 0, res, Integer.SIZE / Byte.SIZE - bytes.length, bytes.length);
120 final ByteBuffer buff = ByteBuffer.wrap(res);
121 return buff.getInt();
125 * Converts byte array to long. If there are less bytes in the array as required (Long.Size), the method will push
126 * adequate number of zero bytes prepending given byte array.
128 * @param bytes array to be converted to long
131 public static long bytesToLong(final byte[] bytes) {
132 if (bytes.length > Long.SIZE / Byte.SIZE) {
133 throw new IllegalArgumentException("Cannot convert bytes to long.Byte array too big.");
135 byte[] res = new byte[Long.SIZE / Byte.SIZE];
136 if (bytes.length != Long.SIZE / Byte.SIZE) {
137 System.arraycopy(bytes, 0, res, Long.SIZE / Byte.SIZE - bytes.length, bytes.length);
141 final ByteBuffer buff = ByteBuffer.wrap(res);
142 return buff.getLong();
146 * Converts byte array to float IEEE 754 format. If there are less bytes in the array as required (Float.Size), the
147 * method will push adequate number of zero bytes prepending given byte array.
149 * @param bytes array to be converted to float
152 public static float bytesToFloat(final byte[] bytes) {
153 if (bytes.length > Float.SIZE / Byte.SIZE) {
154 throw new IllegalArgumentException("Cannot convert bytes to float.Byte array too big.");
156 byte[] res = new byte[Float.SIZE / Byte.SIZE];
157 if (bytes.length != Float.SIZE / Byte.SIZE) {
158 System.arraycopy(bytes, 0, res, Float.SIZE / Byte.SIZE - bytes.length, bytes.length);
162 final ByteBuffer buff = ByteBuffer.wrap(res);
163 return buff.getFloat();
167 * Cuts 'count' number of bytes from the beginning of given byte array.
169 * @param bytes array to be cut, cannot be null
170 * @param count how many bytes needed to be cut, needs to be > 0
171 * @return bytes array without first 'count' bytes
173 public static byte[] cutBytes(final byte[] bytes, final int count) {
174 if (bytes.length == 0 || count > bytes.length || count <= 0) {
175 throw new IllegalArgumentException("Cannot cut bytes, invalid arguments: Count: " + count + " bytes.length: " + bytes.length);
177 return Arrays.copyOfRange(bytes, count, bytes.length);
181 * Parse byte to bits, from the leftmost bit.
183 * @param b byte to be parsed
184 * @return array of booleans with size of 8
186 public static boolean[] parseBits(final byte b) {
187 final boolean[] bits = new boolean[Byte.SIZE];
189 for (int i = Byte.SIZE - 1; i >= 0; i--) {
190 bits[j] = ((b & (1 << i)) != 0);
197 * Parses array of bytes to BitSet, from left most bit.
199 * @param bytes array of bytes to be parsed
200 * @return BitSet with length = bytes.length * Byte.SIZE
202 public static BitSet bytesToBitSet(final byte[] bytes) {
203 final BitSet bitSet = new BitSet(bytes.length * Byte.SIZE);
204 for (int bytesIter = 0; bytesIter < bytes.length; bytesIter++) {
205 final int offset = bytesIter * Byte.SIZE;
206 for (int byteIter = Byte.SIZE - 1; byteIter >= 0; byteIter--) {
207 bitSet.set(offset + (Byte.SIZE - byteIter - 1), (bytes[bytesIter] & 1 << (byteIter)) != 0);
214 * Parses BitSet to bytes, from most left bit.
216 * @param bitSet BitSet to be parsed
217 * @param returnedLength Length of returned array. Overlapping flags are truncated.
218 * @return parsed array of bytes with length of bitSet.length / Byte.SIZE
220 public static byte[] bitSetToBytes(final BitSet bitSet, final int returnedLength) {
221 final byte[] bytes = new byte[returnedLength];
223 for (int bytesIter = 0; bytesIter < bytes.length; bytesIter++) {
224 final int offset = bytesIter * Byte.SIZE;
226 for (int byteIter = Byte.SIZE - 1; byteIter >= 0; byteIter--) {
227 bytes[bytesIter] |= (bitSet.get(offset + (Byte.SIZE - byteIter - 1)) ? 1 << byteIter : 0);
234 * Parses file to array of bytes
236 * @param name path to file to by parsed
237 * @return parsed array of bytes
239 public static byte[] fileToBytes(final String name) throws IOException {
240 final File file = new File(name);
244 if (file.length() > Integer.MAX_VALUE) {
245 throw new IOException("Too large file to load in byte array.");
248 final FileInputStream fin = new FileInputStream(file);
249 final byte[] byteArray = new byte[(int) file.length()];
251 while (offset < byteArray.length && (numRead = fin.read(byteArray, offset, byteArray.length - offset)) >= 0) {
263 * Parses integer to array of bytes
265 * @param num integer to be parsed
266 * @return parsed array of bytes with length of Integer.SIZE/Byte.SIZE
268 public static byte[] intToBytes(final int num) {
269 return intToBytes(num, Integer.SIZE / Byte.SIZE);
273 * Parses integer to array of bytes
275 * @param num integer to be parsed
276 * @param size desired byte array length
277 * @return parsed array of bytes with length of size
279 public static byte[] intToBytes(final int num, final int size) {
280 final int finalSize = Integer.SIZE / Byte.SIZE;
281 final ByteBuffer bytesBuffer = ByteBuffer.allocate(finalSize);
282 bytesBuffer.putInt(num);
283 return ByteArray.subByte(bytesBuffer.array(), finalSize - size, size);
287 * Parses long to array of bytes
289 * @param num long to be parsed
290 * @return parsed array of bytes with length of Long.SIZE/Byte.SIZE
292 public static byte[] longToBytes(final int num) {
293 return longToBytes(num, Long.SIZE / Byte.SIZE);
297 * Parses long to array of bytes
299 * @param num long to be parsed
300 * @param size desired byte array length
301 * @return parsed array of bytes with length of size
303 public static byte[] longToBytes(final long num, final int size) {
304 final int finalSize = Long.SIZE / Byte.SIZE;
305 final ByteBuffer bytesBuffer = ByteBuffer.allocate(finalSize);
306 bytesBuffer.putLong(num);
307 return ByteArray.subByte(bytesBuffer.array(), finalSize - size, size);
311 * Copies range of bits from passed byte and align to right.<br/>
313 * @param src source byte to copy from
314 * @param fromBit bit from which will copy (inclusive) - numbered from 0
315 * @param length of bits to by copied - <1,8>
316 * @return copied value aligned to right
318 public static byte copyBitsRange(final byte src, final int fromBit, final int length) {
319 if (fromBit < 0 | fromBit > Byte.SIZE - 1 | length < 1 | length > Byte.SIZE) {
320 throw new IllegalArgumentException("fromBit or toBit is out of range.");
322 if (fromBit + length > Byte.SIZE) {
323 throw new IllegalArgumentException("Out of range.");
329 for (int i = fromBit + length - 1; i >= fromBit; i--) {
331 if ((src & 1 << (Byte.SIZE - i - 1)) != 0) {
332 retByte |= 1 << retI;
342 * Copies whole source byte array to destination from offset.<br/>
343 * Length of src can't be bigger than dest length minus offset
349 public static void copyWhole(final byte[] src, final byte[] dest, final int offset) {
350 if (dest.length - offset < src.length) {
351 throw new ArrayIndexOutOfBoundsException("Can't copy whole array.");
354 System.arraycopy(src, 0, dest, offset, src.length);
358 * Convert array of bytes to java short.<br/>
359 * Size can't be bigger than size of short in bytes.
361 * @param bytes byte[]
362 * @return array of bytes
364 public static short bytesToShort(final byte[] bytes) {
365 if (bytes.length > Short.SIZE / Byte.SIZE) {
366 throw new IllegalArgumentException("Cannot convert bytes to short. Byte array too big.");
368 byte[] res = new byte[Short.SIZE / Byte.SIZE];
369 if (bytes.length != Short.SIZE / Byte.SIZE) {
370 System.arraycopy(bytes, 0, res, Integer.SIZE / Byte.SIZE - bytes.length, bytes.length);
374 final ByteBuffer buff = ByteBuffer.wrap(res);
375 return buff.getShort();
379 * Convert short java representation to array of bytes.
382 * @return short represented as array of bytes
384 public static byte[] shortToBytes(final short num) {
385 final ByteBuffer bytesBuffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
386 bytesBuffer.putShort(num);
388 return bytesBuffer.array();
392 * Convert float java representation to array of bytes.
395 * @return float represented as array of bytes
397 public static byte[] floatToBytes(final float num) {
398 final ByteBuffer bytesBuffer = ByteBuffer.allocate(Float.SIZE / Byte.SIZE);
399 bytesBuffer.putFloat(num);
401 return bytesBuffer.array();
405 * Pretty print array of bytes as hex encoded string with 16 bytes per line. Each byte is separated by space, after
406 * first 8 bytes there are 2 spaces instead of one.
408 public static String bytesToHexString(final byte[] array) {
409 return bytesToHexString(array, 16, " ", 8, " ");
413 * Pretty-print an array of bytes as hex-encoded string. Separate them with specified separator.
415 public static String toHexString(final byte[] array, final String separator) {
416 final StringBuilder sb = new StringBuilder();
417 for (int i = 0; i < array.length; i++) {
418 sb.append(Hex.encodeHexString(new byte[] { array[i] }));
419 if (i + 1 != array.length) {
420 sb.append(separator);
423 return sb.toString();
427 * Convert array of bytes to hexadecimal String.
430 * @param bytesOnLine number of bytes that should by displayed in one line
431 * @param byteSeparator string that will be placed after each byte
432 * @param wordCount number of bytes that make a 'word' (group of bytes)
433 * @param wordSeparator string that will be placed after each word
434 * @return Hexadecimal string representation of given byte array
436 public static String bytesToHexString(final byte[] array, final int bytesOnLine, final String byteSeparator, final int wordCount,
437 final String wordSeparator) {
438 final StringBuilder sb = new StringBuilder();
439 for (int i = 0; i < array.length; i++) {
440 sb.append(Hex.encodeHexString(new byte[] { array[i] }));
441 if ((i + 1) % bytesOnLine == 0) {
444 sb.append(byteSeparator);
445 if ((i + 1) % wordCount == 0) {
446 sb.append(wordSeparator);
451 return sb.toString();
455 * Decodes bytes to human readable UTF-8 string. If bytes are not valid UTF-8, they are represented as raw binary.
457 * @param bytes bytes to be decoded to string
458 * @return String representation of passed bytes
460 public static String bytesToHRString(final byte[] bytes) {
462 return Charset.forName("UTF-8").newDecoder().decode(ByteBuffer.wrap(bytes)).toString();
463 } catch (final CharacterCodingException e) {
464 return Arrays.toString(bytes);
469 * Searches for byte sequence in given array. Returns the index of first occurrence of this sequence (where it
472 * @param bytes byte array where to search for sequence
473 * @param sequence to be searched in given byte array
474 * @return -1 if the sequence could not be found in given byte array int index of first occurrence of the sequence
477 public static int findByteSequence(final byte[] bytes, final byte[] sequence) {
478 if (bytes.length < sequence.length) {
479 throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
481 if (bytes.length == sequence.length) {
482 if (Arrays.equals(bytes, sequence)) {
489 for (int i = 0; i < bytes.length; i++) {
490 if (bytes[i] == sequence[j]) {
492 if (j == sequence.length) {
502 private static final byte MASK_BITS[] = new byte[] { 0, -128, -64, -32, -16, -8, -4, -2 };
504 public static byte[] maskBytes(final byte[] original, final int bits) {
505 if (original.length * Byte.SIZE < bits) {
506 throw new IllegalArgumentException("Attempted to apply invalid mask (too long)");
509 final int needbytes = (bits + 7) / Byte.SIZE;
510 // We need to have a new copy of the underlying byte array, so that
511 // the original bytes stay untouched
512 final byte[] bytes = Arrays.copyOf(original, original.length);
514 final int needmask = bits % Byte.SIZE;
516 bytes[needbytes - 1] &= MASK_BITS[needmask];
519 // zero-out the rest of the bytes
520 for (int i = needbytes; i < bytes.length; i++) {
527 * Trims zeros from the beginning of the byte array.
530 * @return byte array without leading zeros.
532 public static byte[] trim(final byte[] bytes) {
533 int i = bytes.length - 1;
534 while (i >= 0 && bytes[i] == 0) {
537 return Arrays.copyOf(bytes, i + 1);
541 * Converts given byte array to unsigned Integer.
543 * @param bytes byte array to be converted to unsigned Integer.
546 public static UnsignedInteger bytesToUint32(final byte[] bytes) {
547 Preconditions.checkArgument(bytes.length == Integer.SIZE / Byte.SIZE);
548 return UnsignedInteger.fromIntBits(bytesToInt(bytes));
552 * Converts uint to byte array.
554 * @param uint to be converted to byte array
557 public static byte[] uint32ToBytes(final UnsignedInteger uint) {
558 return intToBytes(uint.intValue());
562 * Converts uint as long to byte array.
564 * @param uint to be converted to byte array
567 public static byte[] uint32ToBytes(final long uint) {
568 return uint32ToBytes(UnsignedInteger.valueOf(uint));