Take advantage of {Integer,Long}.BYTES 08/90808/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 29 Jun 2020 13:42:57 +0000 (15:42 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 29 Jun 2020 13:42:57 +0000 (15:42 +0200)
Rather than diving ourselves, take advantage of Java 8+ constants
exposed as .BYTES.

Change-Id: Iac822ef173fe7f40abed754e26d6563feeeb912d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java
util/src/main/java/org/opendaylight/protocol/util/ByteArray.java

index 2d2fb8f8bc39951ac69ccd5820f78cd297cdad17..598eec38e4ac10076d216e05b1765c9028a15716 100644 (file)
@@ -234,8 +234,7 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
     }
 
     private static byte[] serializeAs4BytesCapability(final As4BytesCapability as4Capability) {
-        final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/
-                + Integer.SIZE / Byte.SIZE /*4 byte value*/);
+        final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/ + Integer.BYTES /*4 byte value*/);
         final As4CapabilityHandler serializer = new As4CapabilityHandler();
         serializer.serializeCapability(new CParametersBuilder().setAs4BytesCapability(as4Capability).build(), buffer);
         return buffer.array();
index dc31906b2f76e3b27df92200f8f146efec89d690..4847456919b349aa0371faa86ffe131451db9d41 100644 (file)
@@ -26,8 +26,6 @@ import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 public final class Stateful07LspUpdateErrorTlvParser implements TlvParser, TlvSerializer {
     public static final int TYPE = 20;
 
-    private static final int CONTENT_LENGTH = Integer.SIZE / Byte.SIZE;
-
     @Override
     public LspErrorCode parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
         if (buffer == null) {
@@ -39,7 +37,7 @@ public final class Stateful07LspUpdateErrorTlvParser implements TlvParser, TlvSe
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         checkArgument(tlv instanceof LspErrorCode, "LspErrorCodeTlv is mandatory.");
-        final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        final ByteBuf body = Unpooled.buffer(Integer.BYTES);
         ByteBufUtils.writeOrZero(body, ((LspErrorCode) tlv).getErrorCode());
         TlvUtil.formatTlv(TYPE, body, buffer);
     }
index 77a0fd6abf6b285721555fcfb7a1c01141815f6b..ab07d5a16c4f8ec91d66148a755ec703f22b3508 100644 (file)
@@ -118,12 +118,11 @@ public final class ByteArray {
      * @return int
      */
     public static int bytesToInt(final byte[] bytes) {
-        checkArgument(bytes.length <= Integer.SIZE / Byte.SIZE,
-                "Cannot convert bytes to integer. Byte array too big.");
+        checkArgument(bytes.length <= Integer.BYTES, "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);
+        if (bytes.length != Integer.BYTES) {
+            res = new byte[Integer.BYTES];
+            System.arraycopy(bytes, 0, res, Integer.BYTES - bytes.length, bytes.length);
         } else {
             res = bytes;
         }
@@ -138,12 +137,11 @@ public final class ByteArray {
      * @return long
      */
     public static long bytesToLong(final byte[] bytes) {
-        checkArgument(bytes.length <= Long.SIZE / Byte.SIZE,
-                "Cannot convert bytes to long.Byte array too big.");
+        checkArgument(bytes.length <= Long.BYTES, "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);
+        if (bytes.length != Long.BYTES) {
+            res = new byte[Long.BYTES];
+            System.arraycopy(bytes, 0, res, Long.BYTES - bytes.length, bytes.length);
         } else {
             res = bytes;
         }