Reduce ByteBuf.writeZero() usage 99/86699/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 4 Jan 2020 14:43:27 +0000 (15:43 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 4 Jan 2020 15:12:51 +0000 (16:12 +0100)
For 1/2/3/4/8-byte output we can use faster methods which put
the appropriately-sized 0 constant into the buffer. Use that.

Change-Id: I2eba6124d1730a7d52a3e992df5542aa080377db
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
23 files changed:
bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/ASGenParser.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/AbstractEsiType.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LacpParser.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LanParser.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/RouterIdParser.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/AbstractEvpnNlri.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/MACIpAdvRParser.java
bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializer.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/GracefulCapabilityHandler.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java
bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/AbstractBmpPerPeerMessageParser.java
pcep/ietf-p2mp-te-lsp/src/main/java/org/opendaylight/protocol/pcep/p2mp/te/lsp/P2MPTeLspCapabilityParser.java
rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/AdminStatusObjectParser.java
rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/FlowSpecObjectParser.java
rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/InformationalFastRerouteObjectParser.java
rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/MetricObjectParser.java
rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/ProtectionCommonParser.java
rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/SenderTspecObjectParser.java
util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java
util/src/test/java/org/opendaylight/protocol/util/ByteBufWriteUtilTest.java

index 81c36eb0db768a2eb0032d1ba463bd7b42ec3e42..0c067c937793ded94275d782d35d63f862bdd1e0 100644 (file)
@@ -25,8 +25,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
  * @author Claudio D. Gasparini
  */
 public final class IpAddressUtil {
-    private static final int ZERO_BYTE = 1;
-
     private IpAddressUtil() {
         throw new UnsupportedOperationException();
     }
@@ -78,7 +76,7 @@ public final class IpAddressUtil {
             body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
             body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
         } else {
-            body.writeZero(ZERO_BYTE);
+            body.writeByte(0);
         }
         return body;
     }
@@ -96,7 +94,7 @@ public final class IpAddressUtil {
         } else if (address.getIpv6Address() != null) {
             body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
         } else {
-            body.writeZero(ZERO_BYTE);
+            body.writeByte(0);
         }
         return body;
     }
index da03b1a9b4aef5d25fde6f89f2313606bdb3e24a..1af115b6aabf6a383899ddc0a9052cfb8c72a438 100644 (file)
@@ -31,7 +31,7 @@ final class ASGenParser extends AbstractEsiType {
         final AsGenerated asGen = ((AsGeneratedCase) esi).getAsGenerated();
         writeUnsignedInt(asGen.getAs().getValue(), body);
         writeUnsignedInt(asGen.getLocalDiscriminator(), body);
-        return body.writeZero(ZERO_BYTE);
+        return body.writeByte(0);
     }
 
     @Override
index 942d95e9feedec92504dada30f2bb1d256c5d8e2..980ba90f0246292c702ffac0bcad540acf3366b6 100644 (file)
@@ -17,7 +17,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
 
 abstract class AbstractEsiType implements EsiParser, EsiSerializer {
     private static final int BODY_LENGTH = 9;
-    static final int ZERO_BYTE = 1;
     static final int MAC_ADDRESS_LENGTH = 6;
 
     @Override
index a2bdc2e5a75049a3ed0da6e8b5b1e69a7cf613ee..cf48c37bf63af08a9dcea73dd3c08f63ab592be7 100644 (file)
@@ -33,7 +33,7 @@ final class LacpParser extends AbstractEsiType {
         final LacpAutoGenerated lacp = ((LacpAutoGeneratedCase) esi).getLacpAutoGenerated();
         body.writeBytes(IetfYangUtil.INSTANCE.bytesFor(lacp.getCeLacpMacAddress()));
         ByteBufWriteUtil.writeUnsignedShort(lacp.getCeLacpPortKey(), body);
-        return body.writeZero(ZERO_BYTE);
+        return body.writeByte(0);
     }
 
     @Override
index 0065a97ccdfd44bc9eae4d29e9d51c0cd64dd131..255553da0b5b7411499d6746e19e96482c697ea7 100644 (file)
@@ -33,7 +33,7 @@ final class LanParser extends AbstractEsiType {
         final LanAutoGenerated lan = ((LanAutoGeneratedCase) esi).getLanAutoGenerated();
         body.writeBytes(IetfYangUtil.INSTANCE.bytesFor(lan.getRootBridgeMacAddress()));
         ByteBufWriteUtil.writeUnsignedShort(lan.getRootBridgePriority(), body);
-        return body.writeZero(ZERO_BYTE);
+        return body.writeByte(0);
     }
 
     @Override
index 6ec1aa5037c3a68d3506ed54a85d4fb14ec01b66..34651725c99b3196f681c75a8571df6a7dc0ccab 100644 (file)
@@ -32,7 +32,7 @@ final class RouterIdParser extends AbstractEsiType {
         final RouterIdGenerated routerID = ((RouterIdGeneratedCase) esi).getRouterIdGenerated();
         ByteBufWriteUtil.writeIpv4Address(routerID.getRouterId(), body);
         ByteBufWriteUtil.writeUnsignedInt(routerID.getLocalDiscriminator(), body);
-        return body.writeZero(ZERO_BYTE);
+        return body.writeByte(0);
     }
 
     @Override
index e78ca3a720b3e5f6569d11f259c338b7efbbaa2a..b035d9ab33ff71b9859a3b5469e0c767ece2890e 100644 (file)
@@ -24,7 +24,6 @@ abstract class AbstractEvpnNlri implements EvpnParser, EvpnSerializer {
     static final int MAC_ADDRESS_LENGTH = 6;
     static final int ESI_SIZE = 10;
     private static final NodeIdentifier ESI_NID = NodeIdentifier.create(Esi.QNAME);
-    static final int ZERO_BYTE = 1;
 
     @Override
     public final ByteBuf serializeEvpn(final EvpnChoice evpn, final ByteBuf common) {
index cea7f0d05bff47512641c4938cc61225360c59c8..74f185a8ff8c8f52a2311f7be3bf9a11cd042012 100644 (file)
@@ -133,10 +133,10 @@ final class MACIpAdvRParser extends AbstractEvpnNlri {
                 body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
                 body.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address()));
             } else {
-                body.writeZero(ZERO_BYTE);
+                body.writeByte(0);
             }
         } else {
-            body.writeZero(ZERO_BYTE);
+            body.writeByte(0);
         }
         return body;
     }
index 691f47189e375297c291a12a52743723c82491dd..9507b2caef42092591150511c9d7858136e60ea7 100644 (file)
@@ -67,8 +67,9 @@ public final class L3vpnMcastNlriSerializer {
                 output.writeByte(IPV6_BITS_LENGTH);
                 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), prefixBuf);
             }
+            // FIXME: remove this funky loop
             while (prefixBuf.readableBytes() % 8 != 0) {
-                prefixBuf.writeZero(1);
+                prefixBuf.writeByte(0);
             }
             output.writeBytes(prefixBuf);
         }
index 978851173f448ae1720de5f17dc06534d459ec90..7e4154eec7b156ed3ac0e728a9b348ba7fcbfd2b 100755 (executable)
@@ -70,10 +70,6 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria
 
     public static final int TYPE = 2;
 
-    private static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
-
-    private static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
-
     private final AttributeRegistry attrReg;
 
     private final NlriRegistry nlriReg;
@@ -97,7 +93,7 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria
             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
             messageBody.writeBytes(withdrawnRoutesBuf);
         } else {
-            messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE);
+            messageBody.writeShort(0);
         }
         if (update.getAttributes() != null) {
             final ByteBuf pathAttributesBuf = Unpooled.buffer();
@@ -105,7 +101,7 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria
             messageBody.writeShort(pathAttributesBuf.writerIndex());
             messageBody.writeBytes(pathAttributesBuf);
         } else {
-            messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE);
+            messageBody.writeShort(0);
         }
         final List<Nlri> nlris = update.getNlri();
         if (nlris != null) {
index 85dd22c193e63321e5ffb713f6a3938449bcdd44..fa5979f9be636639082e893b2dbe75cf7b3a8463 100644 (file)
@@ -86,7 +86,7 @@ public final class GracefulCapabilityHandler implements CapabilityParser, Capabi
             if (t.getAfiFlags() != null && t.getAfiFlags().isForwardingState()) {
                 bytes.writeByte(AFI_FLAG_FORWARDING_STATE);
             } else {
-                bytes.writeZero(1);
+                bytes.writeByte(0);
             }
         }
     }
index 0aab83aca2586f13db634b41c80ddcbdfe7e2476..098d940e966ff9fc1527333af509597b09aba4e7 100644 (file)
@@ -129,7 +129,7 @@ public final class LlGracefulCapabilityHandler implements CapabilityParser, Capa
             if (table.getAfiFlags() != null && table.getAfiFlags().isForwardingState()) {
                 buffer.writeByte(AFI_FLAG_FORWARDING_STATE);
             } else {
-                buffer.writeZero(1);
+                buffer.writeByte(0);
             }
             final Uint24 staleTime = table.getLongLivedStaleTime();
             final int timeval = staleTime != null ? staleTime.getValue().intValue() : 0;
index d215c605532c8902e2882ecaf7aefd2e2c72f7d0..bb4967aa70c9758a35feba9f1f917c05ccaa000d 100644 (file)
@@ -46,7 +46,6 @@ import org.slf4j.LoggerFactory;
 final class SimpleNlriRegistry implements NlriRegistry {
 
     private static final int RESERVED = 1;
-    private static final int NEXT_HOP_LENGHT = 1;
     private static final String PARSER_NOT_FOUND = "Nlri parser not found for table type {}";
     private static final Logger LOG = LoggerFactory.getLogger(SimpleNlriRegistry.class);
 
@@ -186,7 +185,7 @@ final class SimpleNlriRegistry implements NlriRegistry {
             byteAggregator.writeBytes(nextHopBuffer);
 
         } else {
-            byteAggregator.writeZero(NEXT_HOP_LENGHT);
+            byteAggregator.writeByte(0);
         }
         byteAggregator.writeZero(RESERVED);
     }
index ba6593a6c48bd6eee341725efbdd7334f487677f..cfaf7e7c192b9e58c6ca1126aec7c5fc666d7fd1 100644 (file)
@@ -129,12 +129,12 @@ public abstract class AbstractBmpPerPeerMessageParser<T extends Builder<?>> exte
         if (peerHeader.getTimestampSec() != null) {
             ByteBufWriteUtil.writeUnsignedInt(peerHeader.getTimestampSec().getValue(), output);
         } else {
-            output.writeZero(ByteBufWriteUtil.INT_BYTES_LENGTH);
+            output.writeInt(0);
         }
         if (peerHeader.getTimestampMicro() != null) {
             ByteBufWriteUtil.writeUnsignedInt(peerHeader.getTimestampMicro().getValue(), output);
         } else {
-            output.writeZero(ByteBufWriteUtil.INT_BYTES_LENGTH);
+            output.writeInt(0);
         }
     }
 
index 15d6882c5e6d466af75315aeaa6f19719090160a..6298034ff5cd429bb91b4b4334cbe31cc4b3cfbe 100644 (file)
@@ -23,13 +23,13 @@ public final class P2MPTeLspCapabilityParser implements TlvParser, TlvSerializer
     public static final int TYPE = 6;
     private static final int CONTENT_LENGTH = 2;
     @VisibleForTesting
-    static P2mpPceCapability P2MP_CAPABILITY = new P2mpPceCapabilityBuilder().build();
+    static final P2mpPceCapability P2MP_CAPABILITY = new P2mpPceCapabilityBuilder().build();
 
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         Preconditions.checkArgument(tlv instanceof P2mpPceCapability, "P2mpPceCapability is mandatory.");
         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
-        body.writeZero(2);
+        body.writeShort(0);
         TlvUtil.formatTlv(TYPE, body, buffer);
     }
 
index e6e4818db254fbe404fff73d0216884844b834e6..686fdcfd4e988553d1e68d8026c4c4d50cc8553c 100644 (file)
@@ -48,7 +48,7 @@ public final class AdminStatusObjectParser extends AbstractRSVPObjectParser {
         final BitArray reflect = new BitArray(FLAGS_SIZE);
         reflect.set(REFLECT, addObject.isReflect());
         reflect.toByteBuf(output);
-        output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+        output.writeShort(0);
         final BitArray flags = new BitArray(FLAGS_SIZE);
         flags.set(TESTING, addObject.isTesting());
         flags.set(DOWN, addObject.isAdministrativelyDown());
index 32a06aa5bf8657d6de2e6ee72f9366e17f37e9fd..bfd47c47c431d6f1b4c62bddc54b00631ef6e74e 100644 (file)
@@ -71,15 +71,15 @@ public final class FlowSpecObjectParser extends AbstractRSVPObjectParser {
         final int sHeader = flowObj.getServiceHeader().getIntValue();
         if (sHeader == 2) {
             serializeAttributeHeader(BODY_SIZE_REQUESTING, CLASS_NUM, CTYPE, output);
-            output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+            output.writeShort(0);
             output.writeShort(REQUESTING_OVERALL_LENGTH);
         } else {
             serializeAttributeHeader(BODY_SIZE_CONTROLLED, CLASS_NUM, CTYPE, output);
-            output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+            output.writeShort(0);
             output.writeShort(CONTROLLER_OVERALL_LENGHT);
         }
         output.writeByte(sHeader);
-        output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
+        output.writeByte(0);
         if (sHeader == 2) {
             output.writeShort(SERVICE_LENGHT);
         } else {
@@ -87,7 +87,7 @@ public final class FlowSpecObjectParser extends AbstractRSVPObjectParser {
         }
 
         output.writeByte(TOKEN_BUCKET_TSPEC);
-        output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
+        output.writeByte(0);
         output.writeShort(PARAMETER_127_LENGTH);
         final TspecObject tSpec = flowObj.getTspecObject();
         writeFloat32(tSpec.getTokenBucketRate(), output);
@@ -100,7 +100,7 @@ public final class FlowSpecObjectParser extends AbstractRSVPObjectParser {
             return;
         }
         output.writeByte(GUARANTEED_SERVICE_RSPEC);
-        output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
+        output.writeByte(0);
         output.writeShort(PARAMETER_130_LENGTH);
         writeFloat32(flowObj.getRate(), output);
         writeUnsignedInt(flowObj.getSlackTerm(), output);
index 3984aaadf51df101f132c69ef199e9623fc11f22..ecc2761b8799da39643799eba543c18e615c7d44 100644 (file)
@@ -53,7 +53,7 @@ public final class InformationalFastRerouteObjectParser extends AbstractRSVPObje
         byteAggregator.writeByte(fastRerouteObject.getSetupPriority().toJava());
         byteAggregator.writeByte(fastRerouteObject.getHoldPriority().toJava());
         byteAggregator.writeByte(fastRerouteObject.getHopLimit().toJava());
-        byteAggregator.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
+        byteAggregator.writeByte(0);
         byteAggregator.writeBytes(Unpooled.wrappedBuffer(fastRerouteObject.getBandwidth().getValue()));
         writeAttributeFilter(fastRerouteObject.getIncludeAny(), byteAggregator);
         writeAttributeFilter(fastRerouteObject.getExcludeAny(), byteAggregator);
index 39d2abf9b75b25e63d687f22a42e7aee6f90cec7..80c885d9d2cee2f47847c643156c6abd017702e9 100644 (file)
@@ -47,7 +47,7 @@ public final class MetricObjectParser extends AbstractRSVPObjectParser {
         checkArgument(teLspObject instanceof MetricObject, "BandwidthObject is mandatory.");
         final MetricObject metric = (MetricObject) teLspObject;
         serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output);
-        output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+        output.writeShort(0);
         final BitArray reflect = new BitArray(FLAGS_SIZE);
         reflect.set(BOUND, metric.isBound());
         reflect.set(COMPUTED, metric.isComputed());
index 1b115fc0fe52588860cbbd44d220747242af2ada..d73c50918ddea9c4c7e84abb4f7f3de5264779e1 100644 (file)
@@ -20,7 +20,6 @@ import org.slf4j.LoggerFactory;
 
 public class ProtectionCommonParser {
 
-    protected static final int BYTE_SIZE = 1;
     protected static final short PROTECTION_SUBOBJECT_TYPE_1 = 1;
     protected static final short PROTECTION_SUBOBJECT_TYPE_2 = 2;
     protected static final int CONTENT_LENGTH_C2 = 8;
@@ -41,7 +40,7 @@ public class ProtectionCommonParser {
         final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
         flagBitArray.set(SECONDARY, protObj.isSecondary());
         flagBitArray.toByteBuf(output);
-        output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+        output.writeShort(0);
         output.writeByte(protObj.getLinkFlags().getIntValue());
     }
 
@@ -53,14 +52,14 @@ public class ProtectionCommonParser {
         flagBitArray.set(OPERATIONAL, protObj.isOperational());
         flagBitArray.toByteBuf(output);
         output.writeByte(protObj.getLspFlag().getIntValue());
-        output.writeZero(BYTE_SIZE);
+        output.writeByte(0);
         output.writeByte(protObj.getLinkFlags().getIntValue());
         final BitArray flagInPlaceBitArray = new BitArray(FLAGS_SIZE);
         flagInPlaceBitArray.set(IN_PLACE, protObj.isInPlace());
         flagInPlaceBitArray.set(REQUIRED, protObj.isRequired());
         flagInPlaceBitArray.toByteBuf(output);
         output.writeByte(protObj.getSegFlag().getIntValue());
-        output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+        output.writeShort(0);
     }
 
     protected static ProtectionSubobject parseCommonProtectionBodyType2(final ByteBuf byteBuf) throws
@@ -106,7 +105,7 @@ public class ProtectionCommonParser {
 
     protected static void serializeBody(final short ctype, final ProtectionSubobject protObj,
         final ByteBuf output) {
-        output.writeZero(BYTE_SIZE);
+        output.writeByte(0);
         output.writeByte(ctype);
         switch (ctype) {
             case PROTECTION_SUBOBJECT_TYPE_1:
index 9642c7e2fd0524c83546d241d14d0e2c03c6eddf..2b47938921557dd38c0f7e5bf24b799ec94fc1e3 100644 (file)
@@ -26,7 +26,7 @@ public class SenderTspecObjectParser extends AbstractRSVPObjectParser {
     public static final short CLASS_NUM = 12;
     public static final short CTYPE = 2;
     private static final int BODY_SIZE = 32;
-    private static final int SERVICE_LENGHT = 6;
+    private static final int SERVICE_LENGTH = 6;
 
     @Override
     protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
@@ -51,13 +51,14 @@ public class SenderTspecObjectParser extends AbstractRSVPObjectParser {
         checkArgument(teLspObject instanceof TspecObject, "SenderTspecObject is mandatory.");
         final TspecObject tspecObj = (TspecObject) teLspObject;
         serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output);
-        output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
+        output.writeShort(0);
         output.writeShort(OVERALL_LENGTH);
+        // FIXME: this is weird. Use explicit 1?
         output.writeByte(ByteBufWriteUtil.ONE_BYTE_LENGTH);
-        output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
-        output.writeShort(SERVICE_LENGHT);
+        output.writeByte(0);
+        output.writeShort(SERVICE_LENGTH);
         output.writeByte(TOKEN_BUCKET_TSPEC);
-        output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
+        output.writeByte(0);
         output.writeShort(PARAMETER_127_LENGTH);
         writeFloat32(tspecObj.getTokenBucketRate(), output);
         writeFloat32(tspecObj.getTokenBucketSize(), output);
index 6a205886dd0696f7b6545b4258a9260d0019856b..991cca391ad094770e879dee9d84bae6e8071905 100644 (file)
@@ -30,8 +30,6 @@ public final class ByteBufWriteUtil {
 
     public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
 
-    public static final int MEDIUM_BYTES_LENGTH = 3;
-
     public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
 
     public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
@@ -59,11 +57,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeInt(final Integer value, final ByteBuf output) {
-        if (value != null) {
-            output.writeInt(value);
-        } else {
-            output.writeZero(INT_BYTES_LENGTH);
-        }
+        output.writeInt(value != null ? value : 0);
     }
 
     /**
@@ -77,11 +71,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeMedium(final Integer value, final ByteBuf output) {
-        if (value != null) {
-            output.writeMedium(value);
-        } else {
-            output.writeZero(MEDIUM_BYTES_LENGTH);
-        }
+        output.writeMedium(value != null ? value : 0);
     }
 
     /**
@@ -95,11 +85,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeShort(final Short value, final ByteBuf output) {
-        if (value != null) {
-            output.writeShort(value);
-        } else {
-            output.writeZero(SHORT_BYTES_LENGTH);
-        }
+        output.writeShort(value != null ? value : 0);
     }
 
     /**
@@ -113,11 +99,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeLong(final Long value, final ByteBuf output) {
-        if (value != null) {
-            output.writeLong(value);
-        } else {
-            output.writeZero(LONG_BYTES_LENGTH);
-        }
+        output.writeLong(value != null ? value : 0L);
     }
 
     /**
@@ -133,7 +115,7 @@ public final class ByteBufWriteUtil {
         if (value != null) {
             output.writeBoolean(value);
         } else {
-            output.writeZero(ONE_BYTE_LENGTH);
+            output.writeByte(0);
         }
     }
 
@@ -150,11 +132,7 @@ public final class ByteBufWriteUtil {
      */
     @Deprecated(forRemoval = true)
     public static void writeUnsignedByte(final Short value, final ByteBuf output) {
-        if (value != null) {
-            output.writeByte(value);
-        } else {
-            output.writeZero(ONE_BYTE_LENGTH);
-        }
+        output.writeByte(value != null ? value : 0);
     }
 
     /**
@@ -168,11 +146,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
-        if (value != null) {
-            output.writeByte(value.byteValue());
-        } else {
-            output.writeZero(ONE_BYTE_LENGTH);
-        }
+        output.writeByte(value != null ? value.byteValue() : 0);
     }
 
     /**
@@ -188,11 +162,7 @@ public final class ByteBufWriteUtil {
      */
     @Deprecated(forRemoval = true)
     public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
-        if (value != null) {
-            output.writeShort(value.shortValue());
-        } else {
-            output.writeZero(SHORT_BYTES_LENGTH);
-        }
+        output.writeShort(value != null ? value.shortValue() : 0);
     }
 
     /**
@@ -206,11 +176,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
-        if (value != null) {
-            output.writeShort(value.shortValue());
-        } else {
-            output.writeZero(SHORT_BYTES_LENGTH);
-        }
+        output.writeShort(value != null ? value.shortValue() : 0);
     }
 
     /**
@@ -226,11 +192,7 @@ public final class ByteBufWriteUtil {
      */
     @Deprecated(forRemoval = true)
     public static void writeUnsignedInt(final Long value, final ByteBuf output) {
-        if (value != null) {
-            output.writeInt(value.intValue());
-        } else {
-            output.writeZero(INT_BYTES_LENGTH);
-        }
+        output.writeInt(value != null ? value.intValue() : 0);
     }
 
     /**
@@ -244,11 +206,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
-        if (value != null) {
-            output.writeInt(value.intValue());
-        } else {
-            output.writeZero(INT_BYTES_LENGTH);
-        }
+        output.writeInt(value != null ? value.intValue() : 0);
     }
 
     /**
@@ -264,11 +222,7 @@ public final class ByteBufWriteUtil {
      */
     @Deprecated(forRemoval = true)
     public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
-        if (value != null) {
-            output.writeLong(value.longValue());
-        } else {
-            output.writeZero(LONG_BYTES_LENGTH);
-        }
+        output.writeLong(value != null ? value.longValue() : 0L);
     }
 
     /**
@@ -282,11 +236,7 @@ public final class ByteBufWriteUtil {
      *            ByteBuf, where value or zeros are written.
      */
     public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
-        if (value != null) {
-            output.writeLong(value.longValue());
-        } else {
-            output.writeZero(LONG_BYTES_LENGTH);
-        }
+        output.writeLong(value != null ? value.longValue() : 0L);
     }
 
     /**
@@ -302,7 +252,7 @@ public final class ByteBufWriteUtil {
         if (ipv4Address != null) {
             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
         } else {
-            output.writeZero(Ipv4Util.IP4_LENGTH);
+            output.writeInt(0);
         }
     }
 
@@ -319,7 +269,7 @@ public final class ByteBufWriteUtil {
         if (ipv4Address != null) {
             output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
         } else {
-            output.writeZero(Ipv4Util.IP4_LENGTH);
+            output.writeInt(0);
         }
     }
 
@@ -421,7 +371,7 @@ public final class ByteBufWriteUtil {
         if (value != null) {
             output.writeBytes(value.getValue());
         } else {
-            output.writeZero(FLOAT32_BYTES_LENGTH);
+            output.writeInt(0);
         }
     }
 }
index e6f25e8d32bd90709cd19077948bcfe346d121a4..1a6db061dd005a08c85940d62008cdbc428b9d9d 100644 (file)
@@ -74,7 +74,7 @@ public class ByteBufWriteUtilTest {
     @Test
     public void testWriteMediumValue() {
         final byte[] result = { 0, 0, 5 };
-        final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.MEDIUM_BYTES_LENGTH);
+        final ByteBuf output = Unpooled.buffer(3);
         writeMedium(5, output);
         assertArrayEquals(result, output.array());