Remove useless UnsupportedOperationExceptions
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / ByteArray.java
index 10f7f2424130e355ad36e7b4f064a4747390a720..77a0fd6abf6b285721555fcfb7a1c01141815f6b 100644 (file)
@@ -7,7 +7,8 @@
  */
 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;
@@ -16,7 +17,7 @@ import java.nio.ByteBuffer;
 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;
 
@@ -24,11 +25,10 @@ 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
     }
 
     /**
@@ -40,8 +40,8 @@ public final class ByteArray {
      * @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;
@@ -67,8 +67,8 @@ public final class ByteArray {
      * @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;
@@ -95,7 +95,8 @@ public final class ByteArray {
      * @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;
@@ -106,7 +107,7 @@ public final class ByteArray {
     }
 
     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;
     }
 
     /**
@@ -117,7 +118,8 @@ public final class ByteArray {
      * @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.");
+        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];
@@ -136,7 +138,8 @@ public final class ByteArray {
      * @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.");
+        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];
@@ -155,68 +158,13 @@ public final class ByteArray {
      * @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
@@ -224,13 +172,13 @@ public final class ByteArray {
     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) {
@@ -251,15 +199,16 @@ public final class ByteArray {
      * @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;
             }
 
@@ -283,4 +232,14 @@ public final class ByteArray {
             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));
+    }
 }