From 5a392643728c6153d4d856629519ac7724a000d9 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 29 Jun 2020 15:42:57 +0200 Subject: [PATCH] Take advantage of {Integer,Long}.BYTES Rather than diving ourselves, take advantage of Java 8+ constants exposed as .BYTES. Change-Id: Iac822ef173fe7f40abed754e26d6563feeeb912d Signed-off-by: Robert Varga --- .../bgp/rib/impl/StrictBGPPeerRegistry.java | 3 +-- .../Stateful07LspUpdateErrorTlvParser.java | 4 +--- .../opendaylight/protocol/util/ByteArray.java | 18 ++++++++---------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java index 2d2fb8f8bc..598eec38e4 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java @@ -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(); diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java index dc31906b2f..4847456919 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java @@ -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); } diff --git a/util/src/main/java/org/opendaylight/protocol/util/ByteArray.java b/util/src/main/java/org/opendaylight/protocol/util/ByteArray.java index 77a0fd6abf..ab07d5a16c 100644 --- a/util/src/main/java/org/opendaylight/protocol/util/ByteArray.java +++ b/util/src/main/java/org/opendaylight/protocol/util/ByteArray.java @@ -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; } -- 2.36.6