*/
package org.opendaylight.protocol.util;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
import io.netty.buffer.ByteBuf;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
-import java.util.BitSet;
+import java.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* Util class for methods working with byte array.
*/
public final class ByteArray {
-
private static final Logger LOG = LoggerFactory.getLogger(ByteArray.class);
private ByteArray() {
- throw new UnsupportedOperationException();
+ // Hidden on purpose
}
/**
* @return byte array
*/
public static byte[] readBytes(final ByteBuf buffer, final int length) {
- Preconditions.checkArgument(buffer != null && buffer.readableBytes() >= length,
- "Buffer cannot be read for %s bytes.", length);
+ checkArgument(buffer != null && buffer.readableBytes() >= length,
+ "Buffer cannot be read for %s bytes.", length);
final byte[] result = new byte[length];
buffer.readBytes(result);
return result;
* @return byte array
*/
public static byte[] getBytes(final ByteBuf buffer, final int length) {
- Preconditions.checkArgument(buffer != null && buffer.readableBytes() >= length,
- "Buffer cannot be read for %s bytes.", length);
+ checkArgument(buffer != null && buffer.readableBytes() >= length,
+ "Buffer cannot be read for %s bytes.", length);
final byte[] result = new byte[length];
buffer.getBytes(buffer.readerIndex(), result);
return result;
* @return a new byte array that is a sub-array of the original
*/
public static byte[] subByte(final byte[] bytes, final int startIndex, final int length) {
- Preconditions.checkArgument(checkLength(bytes, length) && checkStartIndex(bytes, startIndex, length), "Cannot create subByte, invalid arguments: Length: %s startIndex: %s", length, startIndex);
+ checkArgument(checkLength(bytes, length) && checkStartIndex(bytes, startIndex, length),
+ "Cannot create subByte, invalid arguments: Length: %s startIndex: %s", length, startIndex);
final byte[] res = new byte[length];
System.arraycopy(bytes, startIndex, res, 0, length);
return res;
}
private static boolean checkStartIndex(final byte[] bytes, final int startIndex, final int length) {
- return startIndex >= 0 && startIndex < bytes.length && (startIndex + length <= bytes.length);
+ return startIndex >= 0 && startIndex < bytes.length && startIndex + length <= bytes.length;
}
/**
* @return int
*/
public static int bytesToInt(final byte[] bytes) {
- Preconditions.checkArgument(bytes.length <= Integer.SIZE / Byte.SIZE, "Cannot convert bytes to integer. Byte array too big.");
- byte[] res = new byte[Integer.SIZE / Byte.SIZE];
+ checkArgument(bytes.length <= Integer.SIZE / Byte.SIZE,
+ "Cannot convert bytes to integer. Byte array too big.");
+ final byte[] res;
if (bytes.length != Integer.SIZE / Byte.SIZE) {
+ res = new byte[Integer.SIZE / Byte.SIZE];
System.arraycopy(bytes, 0, res, Integer.SIZE / Byte.SIZE - bytes.length, bytes.length);
} else {
res = bytes;
}
- final ByteBuffer buff = ByteBuffer.wrap(res);
- return buff.getInt();
+ return ByteBuffer.wrap(res).getInt();
}
/**
* @return long
*/
public static long bytesToLong(final byte[] bytes) {
- Preconditions.checkArgument(bytes.length <= Long.SIZE / Byte.SIZE, "Cannot convert bytes to long.Byte array too big.");
- byte[] res = new byte[Long.SIZE / Byte.SIZE];
+ checkArgument(bytes.length <= Long.SIZE / Byte.SIZE,
+ "Cannot convert bytes to long.Byte array too big.");
+ final byte[] res;
if (bytes.length != Long.SIZE / Byte.SIZE) {
+ res = new byte[Long.SIZE / Byte.SIZE];
System.arraycopy(bytes, 0, res, Long.SIZE / Byte.SIZE - bytes.length, bytes.length);
} else {
res = bytes;
}
- final ByteBuffer buff = ByteBuffer.wrap(res);
- return buff.getLong();
+ return ByteBuffer.wrap(res).getLong();
}
/**
* @return bytes array without first 'count' bytes
*/
public static byte[] cutBytes(final byte[] bytes, final int count) {
- Preconditions.checkArgument(bytes.length != 0 && count <= bytes.length && count > 0, "Cannot cut bytes, invalid arguments: Count: %s bytes.length: %s", count, bytes.length);
+ checkArgument(bytes.length != 0 && count <= bytes.length && count > 0,
+ "Cannot cut bytes, invalid arguments: Count: %s bytes.length: %s", count, bytes.length);
return Arrays.copyOfRange(bytes, count, bytes.length);
}
/**
- * Parse byte to bits, from the leftmost bit.
- *
- * @param b byte to be parsed
- * @return array of booleans with size of 8
- */
- @Deprecated
- public static boolean[] parseBits(final byte b) {
- final boolean[] bits = new boolean[Byte.SIZE];
- int j = 0;
- for (int i = Byte.SIZE - 1; i >= 0; i--) {
- bits[j] = ((b & (1 << i)) != 0);
- j++;
- }
- return bits;
- }
-
- /**
- * Parses array of bytes to BitSet, from left most bit.
- *
- * @param bytes array of bytes to be parsed
- * @return BitSet with length = bytes.length * Byte.SIZE
- */
- @Deprecated
- public static BitSet bytesToBitSet(final byte[] bytes) {
- final BitSet bitSet = new BitSet(bytes.length * Byte.SIZE);
- for (int bytesIter = 0; bytesIter < bytes.length; bytesIter++) {
- final int offset = bytesIter * Byte.SIZE;
- for (int byteIter = Byte.SIZE - 1; byteIter >= 0; byteIter--) {
- bitSet.set(offset + (Byte.SIZE - byteIter - 1), (bytes[bytesIter] & 1 << (byteIter)) != 0);
- }
- }
- return bitSet;
- }
-
- /**
- * Parses BitSet to bytes, from most left bit.
- *
- * @param bitSet BitSet to be parsed
- * @param returnedLength Length of returned array. Overlapping flags are truncated.
- * @return parsed array of bytes with length of bitSet.length / Byte.SIZE
- */
- @Deprecated
- public static byte[] bitSetToBytes(final BitSet bitSet, final int returnedLength) {
- final byte[] bytes = new byte[returnedLength];
-
- for (int bytesIter = 0; bytesIter < bytes.length; bytesIter++) {
- final int offset = bytesIter * Byte.SIZE;
-
- for (int byteIter = Byte.SIZE - 1; byteIter >= 0; byteIter--) {
- bytes[bytesIter] |= (bitSet.get(offset + (Byte.SIZE - byteIter - 1)) ? 1 << byteIter : 0);
- }
- }
- return bytes;
- }
-
- /**
- * Parses file to array of bytes
+ * Parses file to array of bytes.
*
* @param name path to file to by parsed
* @return parsed array of bytes
public static byte[] fileToBytes(final String name) throws IOException {
final File file = new File(name);
int offset = 0;
- int numRead = 0;
+ int numRead;
if (file.length() > Integer.MAX_VALUE) {
throw new IOException("Too large file to load in byte array.");
}
final byte[] byteArray = new byte[(int) file.length()];
- try (final FileInputStream fin = new FileInputStream(file)) {
+ try (FileInputStream fin = new FileInputStream(file)) {
while (offset < byteArray.length) {
numRead = fin.read(byteArray, offset, byteArray.length - offset);
if (numRead >= 0) {
* @return copied value aligned to right
*/
public static byte copyBitsRange(final byte src, final int fromBit, final int length) {
- Preconditions.checkArgument(fromBit >= 0 && fromBit <= Byte.SIZE - 1 && length >= 1 && length <= Byte.SIZE, "fromBit or toBit is out of range.");
- Preconditions.checkArgument(fromBit + length <= Byte.SIZE, "Out of range.");
+ checkArgument(fromBit >= 0 && fromBit <= Byte.SIZE - 1 && length >= 1 && length <= Byte.SIZE,
+ "fromBit or toBit is out of range.");
+ checkArgument(fromBit + length <= Byte.SIZE, "Out of range.");
byte retByte = 0;
int retI = 0;
for (int i = fromBit + length - 1; i >= fromBit; i--) {
- if ((src & 1 << (Byte.SIZE - i - 1)) != 0) {
+ if ((src & 1 << Byte.SIZE - i - 1) != 0) {
retByte |= 1 << retI;
}
return Arrays.toString(bytes);
}
}
+
+ /**
+ * Encode input ByteBuf with Base64 to string format.
+ *
+ * @param buffer Input ByteBuf
+ * @return String representation of encoded ByteBuf.
+ */
+ public static String encodeBase64(final ByteBuf buffer) {
+ return Base64.getEncoder().encodeToString(ByteArray.readAllBytes(buffer));
+ }
}