Class ByteBufUtils
- java.lang.Object
-
- org.opendaylight.yangtools.yang.common.netty.ByteBufUtils
-
@Beta public final class ByteBufUtils extends Object
Utility methods for interacting withByteBuf
s. 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 theUint8.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 anIllegalArgumentException
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 aUint16
from specified buffer.static @NonNull Uint32
readUint32(io.netty.buffer.ByteBuf buf)
Read aUint32
from specified buffer.static @NonNull Uint64
readUint64(io.netty.buffer.ByteBuf buf)
Read aUint64
from specified buffer.static @NonNull Uint8
readUint8(io.netty.buffer.ByteBuf buf)
Read anUint8
from specified buffer.static void
write(io.netty.buffer.ByteBuf buf, Uint16 value)
Write aUint16
to specified buffer.static void
write(io.netty.buffer.ByteBuf buf, Uint32 value)
Write aUint32
from specified buffer.static void
write(io.netty.buffer.ByteBuf buf, Uint64 value)
Write aUint64
to specified buffer.static void
write(io.netty.buffer.ByteBuf buf, Uint8 value)
Write aUint8
to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Byte value, String name)
Write aByte
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Integer value, String name)
Write aInteger
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Long value, String name)
Write aLong
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Short value, String name)
Write aShort
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Uint16 value, String name)
Write aUint16
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Uint32 value, String name)
Write aUint32
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Uint64 value, String name)
Write aUint64
property to specified buffer.static void
writeMandatory(io.netty.buffer.ByteBuf buf, Uint8 value, String name)
Write aUint8
property to specified buffer.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
Write aByte
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
Write aInteger
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Long value)
Write aLong
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Short value)
Write aByte
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
Write aUint16
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
Write aUint32
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
Write aUint64
value to specified buffer if it is not null.static void
writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
Write aUint8
value to specified buffer if it is not null.static void
writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
Write aByte
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 aByte
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 aByte
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 aByte
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 aByte
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 aByte
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 aByte
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 aByte
value to specified buffer if it is not null, otherwise write one zero byte.
-
-
-
Method Detail
-
readUint8
public static @NonNull Uint8 readUint8(io.netty.buffer.ByteBuf buf)
Read anUint8
from specified buffer.- Parameters:
buf
- buffer- Returns:
- A
Uint8
- Throws:
NullPointerException
- ifbuf
is nullIndexOutOfBoundsException
- ifbuf
does not have enough data
-
readUint16
public static @NonNull Uint16 readUint16(io.netty.buffer.ByteBuf buf)
Read aUint16
from specified buffer.- Parameters:
buf
- buffer- Returns:
- A
Uint16
- Throws:
NullPointerException
- ifbuf
is nullIndexOutOfBoundsException
- ifbuf
does not have enough data
-
readUint32
public static @NonNull Uint32 readUint32(io.netty.buffer.ByteBuf buf)
Read aUint32
from specified buffer.- Parameters:
buf
- buffer- Returns:
- A
Uint32
- Throws:
NullPointerException
- ifbuf
is nullIndexOutOfBoundsException
- ifbuf
does not have enough data
-
readUint64
public static @NonNull Uint64 readUint64(io.netty.buffer.ByteBuf buf)
Read aUint64
from specified buffer.- Parameters:
buf
- buffer- Returns:
- A
Uint64
- Throws:
NullPointerException
- ifbuf
is nullIndexOutOfBoundsException
- ifbuf
does not have enough data
-
write
public static void write(io.netty.buffer.ByteBuf buf, Uint8 value)
Write aUint8
to specified buffer.- Parameters:
buf
- buffervalue
- AUint8
- Throws:
NullPointerException
- if any argument is null
-
write
public static void write(io.netty.buffer.ByteBuf buf, Uint16 value)
Write aUint16
to specified buffer.- Parameters:
buf
- buffervalue
- AUint16
- Throws:
NullPointerException
- if any argument is null
-
write
public static void write(io.netty.buffer.ByteBuf buf, Uint32 value)
Write aUint32
from specified buffer.- Parameters:
buf
- buffervalue
- AUint32
- Throws:
NullPointerException
- if any argument is null
-
write
public static void write(io.netty.buffer.ByteBuf buf, Uint64 value)
Write aUint64
to specified buffer.- Parameters:
buf
- buffervalue
- AUint64
- Throws:
NullPointerException
- if any argument is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Byte value, String name)
Write aByte
property to specified buffer. If thevalue
is known to be non-null, preferByteBuf.writeByte(int)
instead of this method.- Parameters:
buf
- buffervalue
- AByte
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Short value, String name)
Write aShort
property to specified buffer. If thevalue
is known to be non-null, preferByteBuf.writeShort(int)
instead of this method.- Parameters:
buf
- buffervalue
- AShort
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Integer value, String name)
Write aInteger
property to specified buffer. If thevalue
is known to be non-null, preferByteBuf.writeInt(int)
instead of this method.- Parameters:
buf
- buffervalue
- AInteger
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Long value, String name)
Write aLong
property to specified buffer. If thevalue
is known to be non-null, preferByteBuf.writeLong(long)
instead of this method.- Parameters:
buf
- buffervalue
- ALong
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint8 value, String name)
Write aUint8
property to specified buffer. If thevalue
is known to be non-null, prefer to usewrite(ByteBuf, Uint8)
instead of this method.- Parameters:
buf
- buffervalue
- AUint8
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint16 value, String name)
Write aUint16
property to specified buffer. If thevalue
is known to be non-null, prefer to usewrite(ByteBuf, Uint16)
instead of this method.- Parameters:
buf
- buffervalue
- AUint16
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint32 value, String name)
Write aUint32
property to specified buffer. If thevalue
is known to be non-null, prefer to usewrite(ByteBuf, Uint32)
instead of this method.- Parameters:
buf
- buffervalue
- AUint32
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeMandatory
public static void writeMandatory(io.netty.buffer.ByteBuf buf, Uint64 value, String name)
Write aUint64
property to specified buffer. If thevalue
is known to be non-null, prefer to usewrite(ByteBuf, Uint64)
instead of this method.- Parameters:
buf
- buffervalue
- AUint64
name
- Property name for error reporting purposes- Throws:
NullPointerException
- ifbuf
is nullIllegalArgumentException
- ifvalue
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
Write aByte
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Short value)
Write aByte
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AShort
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
Write aInteger
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AInteger
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Long value)
Write aLong
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- ALong
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
Write aUint8
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AUint8
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
Write aUint16
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AUint16
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
Write aUint32
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AUint32
- Throws:
NullPointerException
- ifbuf
is null
-
writeOptional
public static void writeOptional(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
Write aUint64
value to specified buffer if it is not null.- Parameters:
buf
- buffervalue
- AUint64
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Byte value)
Write aByte
value to specified buffer if it is not null, otherwise write one zero byte.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Short value)
Write aByte
value to specified buffer if it is not null, otherwise write two zero bytes.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Integer value)
Write aByte
value to specified buffer if it is not null, otherwise write four zero bytes.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Long value)
Write aByte
value to specified buffer if it is not null, otherwise write eight zero bytes.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint8 value)
Write aByte
value to specified buffer if it is not null, otherwise write one zero byte.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint16 value)
Write aByte
value to specified buffer if it is not null, otherwise write two zero bytes.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint32 value)
Write aByte
value to specified buffer if it is not null, otherwise write four zero bytes.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
writeOrZero
public static void writeOrZero(io.netty.buffer.ByteBuf buf, @Nullable Uint64 value)
Write aByte
value to specified buffer if it is not null, otherwise write eight zero bytes.- Parameters:
buf
- buffervalue
- AByte
- Throws:
NullPointerException
- ifbuf
is null
-
-