Class ByteBufUtils


  • @Beta
    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

      All Methods Static Methods Concrete Methods 
      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 from 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.
    • Method Detail

      • write

        public static void write​(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
      • write

        public static void write​(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
      • write

        public static void write​(io.netty.buffer.ByteBuf buf,
                                 Uint32 value)
        Write a Uint32 from specified buffer.
        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.
        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
      • 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