Class ByteBufUtils

java.lang.Object
org.opendaylight.yangtools.yang.common.netty.ByteBufUtils

public final class ByteBufUtils extends Object
Utility methods for interacting with ByteBufs. These add a number of methods for reading and writing various data types from/to ByteBufs. Methods fall into these categories:
  • readUint*, which extract the corresponding amount of data from a buffer and return an Uint type. These are more efficient than going the Uint8.valueOf(buf.readUnsignedByte()) route.
  • writeUint*, which write specified value into a buffer.
  • writeMandatory*, which write a property not statically known to be non-null. These methods throw an IllegalArgumentException if the supplied value is null. Otherwise they will write it to provided buffer.
  • writeOptional*, which write a value which can legally be null. In case the value is not null, it is written to the provided buffer. If the value null, the method does nothing.
  • writeOrZero*, which write a value which can legally be null. In case the value is not null, it is written to the provided buffer. If the value is null, a {code zero} value of corresponding width is written instead.
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NonNull Uint16
    readUint16(io.netty.buffer.ByteBuf buf)
    Read a Uint16 from specified buffer.
    static @NonNull Uint32
    readUint32(io.netty.buffer.ByteBuf buf)
    Read a Uint32 from specified buffer.
    static @NonNull Uint64
    readUint64(io.netty.buffer.ByteBuf buf)
    Read a Uint64 from specified buffer.
    static @NonNull Uint8
    readUint8(io.netty.buffer.ByteBuf buf)
    Read an Uint8 from specified buffer.
    static void
    write(io.netty.buffer.ByteBuf buf, Uint16 value)
    Write a Uint16 to specified buffer.
    static void
    write(io.netty.buffer.ByteBuf buf, Uint32 value)
    Write a Uint32 to specified buffer.
    static void
    write(io.netty.buffer.ByteBuf buf, Uint64 value)
    Write a Uint64 to specified buffer.
    static void
    write(io.netty.buffer.ByteBuf buf, Uint8 value)
    Write a Uint8 to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Byte value, String name)
    Write a Byte property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Integer value, String name)
    Write a Integer property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Long value, String name)
    Write a Long property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Short value, String name)
    Write a Short property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Uint16 value, String name)
    Write a Uint16 property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Uint32 value, String name)
    Write a Uint32 property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Uint64 value, String name)
    Write a Uint64 property to specified buffer.
    static void
    writeMandatory(io.netty.buffer.ByteBuf buf, Uint8 value, String name)
    Write a Uint8 property to specified buffer.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
    Write a Byte value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
    Write a Integer value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Long value)
    Write a Long value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Short value)
    Write a Byte value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
    Write a Uint16 value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
    Write a Uint32 value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
    Write a Uint64 value to specified buffer if it is not null.
    static void
    writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
    Write a Uint8 value to specified buffer if it is not null.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
    Write a Byte value to specified buffer if it is not null, otherwise write one zero byte.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
    Write a Integer value to specified buffer if it is not null, otherwise write four zero bytes.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Long value)
    Write a Byte value to specified buffer if it is not null, otherwise write eight zero bytes.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Short value)
    Write a Short value to specified buffer if it is not null, otherwise write two zero bytes.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
    Write a Uint16 value to specified buffer if it is not null, otherwise write two zero bytes.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
    Write a Uint32 value to specified buffer if it is not null, otherwise write four zero bytes.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
    Write a Uint64 value to specified buffer if it is not null, otherwise write eight zero bytes.
    static void
    writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
    Write a Uint8 value to specified buffer if it is not null, otherwise write one zero byte.
    static void
    writeUint16(io.netty.buffer.ByteBuf buf, Uint16 value)
    Write a Uint16 to specified buffer.
    static void
    writeUint32(io.netty.buffer.ByteBuf buf, Uint32 value)
    Write a Uint32 to specified buffer.
    static void
    writeUint64(io.netty.buffer.ByteBuf buf, Uint64 value)
    Write a Uint64 to specified buffer.
    static void
    writeUint8(io.netty.buffer.ByteBuf buf, Uint8 value)
    Write a Uint8 to specified buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • readUint8

      public static @NonNull Uint8 readUint8(io.netty.buffer.ByteBuf buf)
      Read an Uint8 from specified buffer.
      Parameters:
      buf - buffer
      Returns:
      A Uint8
      Throws:
      NullPointerException - if buf is null
      IndexOutOfBoundsException - if buf does not have enough data
    • readUint16

      public static @NonNull Uint16 readUint16(io.netty.buffer.ByteBuf buf)
      Read a Uint16 from specified buffer.
      Parameters:
      buf - buffer
      Returns:
      A Uint16
      Throws:
      NullPointerException - if buf is null
      IndexOutOfBoundsException - if buf does not have enough data
    • readUint32

      public static @NonNull Uint32 readUint32(io.netty.buffer.ByteBuf buf)
      Read a Uint32 from specified buffer.
      Parameters:
      buf - buffer
      Returns:
      A Uint32
      Throws:
      NullPointerException - if buf is null
      IndexOutOfBoundsException - if buf does not have enough data
    • readUint64

      public static @NonNull Uint64 readUint64(io.netty.buffer.ByteBuf buf)
      Read a Uint64 from specified buffer.
      Parameters:
      buf - buffer
      Returns:
      A Uint64
      Throws:
      NullPointerException - if buf is null
      IndexOutOfBoundsException - if buf does not have enough data
    • writeUint8

      public static void writeUint8(io.netty.buffer.ByteBuf buf, Uint8 value)
      Write a Uint8 to specified buffer.
      Parameters:
      buf - buffer
      value - A Uint8
      Throws:
      NullPointerException - if any argument is null
    • writeUint16

      public static void writeUint16(io.netty.buffer.ByteBuf buf, Uint16 value)
      Write a Uint16 to specified buffer.
      Parameters:
      buf - buffer
      value - A Uint16
      Throws:
      NullPointerException - if any argument is null
    • writeUint32

      public static void writeUint32(io.netty.buffer.ByteBuf buf, Uint32 value)
      Write a Uint32 to specified buffer.
      Parameters:
      buf - buffer
      value - A Uint32
      Throws:
      NullPointerException - if any argument is null
    • writeUint64

      public static void writeUint64(io.netty.buffer.ByteBuf buf, Uint64 value)
      Write a Uint64 to specified buffer.
      Parameters:
      buf - buffer
      value - A Uint64
      Throws:
      NullPointerException - if any argument is null
    • write

      public static void write(io.netty.buffer.ByteBuf buf, Uint8 value)
      Write a Uint8 to specified buffer. This method is provided for convenience, you may want to use writeUint8(ByteBuf, Uint8) as it is more explicit.
      Parameters:
      buf - buffer
      value - A Uint8
      Throws:
      NullPointerException - if any argument is null
    • write

      public static void write(io.netty.buffer.ByteBuf buf, Uint16 value)
      Write a Uint16 to specified buffer. This method is provided for convenience, you may want to use writeUint16(ByteBuf, Uint16) as it is more explicit.
      Parameters:
      buf - buffer
      value - A Uint16
      Throws:
      NullPointerException - if any argument is null
    • write

      public static void write(io.netty.buffer.ByteBuf buf, Uint32 value)
      Write a Uint32 to specified buffer. This method is provided for convenience, you may want to use writeUint32(ByteBuf, Uint32) as it is more explicit.
      Parameters:
      buf - buffer
      value - A Uint32
      Throws:
      NullPointerException - if any argument is null
    • write

      public static void write(io.netty.buffer.ByteBuf buf, Uint64 value)
      Write a Uint64 to specified buffer. This method is provided for convenience, you may want to use writeUint64(ByteBuf, Uint64) as it is more explicit.
      Parameters:
      buf - buffer
      value - A Uint64
      Throws:
      NullPointerException - if any argument is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Byte value, String name)
      Write a Byte property to specified buffer. If the value is known to be non-null, prefer ByteBuf.writeByte(int) instead of this method.
      Parameters:
      buf - buffer
      value - A Byte
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Short value, String name)
      Write a Short property to specified buffer. If the value is known to be non-null, prefer ByteBuf.writeShort(int) instead of this method.
      Parameters:
      buf - buffer
      value - A Short
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Integer value, String name)
      Write a Integer property to specified buffer. If the value is known to be non-null, prefer ByteBuf.writeInt(int) instead of this method.
      Parameters:
      buf - buffer
      value - A Integer
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Long value, String name)
      Write a Long property to specified buffer. If the value is known to be non-null, prefer ByteBuf.writeLong(long) instead of this method.
      Parameters:
      buf - buffer
      value - A Long
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint8 value, String name)
      Write a Uint8 property to specified buffer. If the value is known to be non-null, prefer to use write(ByteBuf, Uint8) instead of this method.
      Parameters:
      buf - buffer
      value - A Uint8
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint16 value, String name)
      Write a Uint16 property to specified buffer. If the value is known to be non-null, prefer to use write(ByteBuf, Uint16) instead of this method.
      Parameters:
      buf - buffer
      value - A Uint16
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint32 value, String name)
      Write a Uint32 property to specified buffer. If the value is known to be non-null, prefer to use write(ByteBuf, Uint32) instead of this method.
      Parameters:
      buf - buffer
      value - A Uint32
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeMandatory

      public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint64 value, String name)
      Write a Uint64 property to specified buffer. If the value is known to be non-null, prefer to use write(ByteBuf, Uint64) instead of this method.
      Parameters:
      buf - buffer
      value - A Uint64
      name - Property name for error reporting purposes
      Throws:
      NullPointerException - if buf is null
      IllegalArgumentException - if value is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
      Write a Byte value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Byte
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Short value)
      Write a Byte value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Short
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
      Write a Integer value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Integer
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Long value)
      Write a Long value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Long
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
      Write a Uint8 value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Uint8
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
      Write a Uint16 value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Uint16
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
      Write a Uint32 value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Uint32
      Throws:
      NullPointerException - if buf is null
    • writeOptional

      public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
      Write a Uint64 value to specified buffer if it is not null.
      Parameters:
      buf - buffer
      value - A Uint64
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
      Write a Byte value to specified buffer if it is not null, otherwise write one zero byte.
      Parameters:
      buf - buffer
      value - A Byte
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Short value)
      Write a Short value to specified buffer if it is not null, otherwise write two zero bytes.
      Parameters:
      buf - buffer
      value - A Short
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
      Write a Integer value to specified buffer if it is not null, otherwise write four zero bytes.
      Parameters:
      buf - buffer
      value - A Integer
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Long value)
      Write a Byte value to specified buffer if it is not null, otherwise write eight zero bytes.
      Parameters:
      buf - buffer
      value - A Byte
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
      Write a Uint8 value to specified buffer if it is not null, otherwise write one zero byte.
      Parameters:
      buf - buffer
      value - A Uint8
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
      Write a Uint16 value to specified buffer if it is not null, otherwise write two zero bytes.
      Parameters:
      buf - buffer
      value - A Uint16
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
      Write a Uint32 value to specified buffer if it is not null, otherwise write four zero bytes.
      Parameters:
      buf - buffer
      value - A Uint32
      Throws:
      NullPointerException - if buf is null
    • writeOrZero

      public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
      Write a Uint64 value to specified buffer if it is not null, otherwise write eight zero bytes.
      Parameters:
      buf - buffer
      value - A Uint64
      Throws:
      NullPointerException - if buf is null