Class NettyImmutableDataBuf
java.lang.Object
eu.cloudnetservice.driver.network.netty.buffer.NettyImmutableDataBuf
- All Implemented Interfaces:
DataBuf,AutoCloseable
- Direct Known Subclasses:
NettyMutableDataBuf
The default implementation of an immutable data buf wrapping a netty byte buf.
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface eu.cloudnetservice.driver.network.buffer.DataBuf
DataBuf.Mutable -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final io.netty5.buffer.Bufferprotected intprotected booleanprotected int -
Constructor Summary
ConstructorsConstructorDescriptionNettyImmutableDataBuf(@NonNull io.netty5.buffer.Buffer buffer) Constructs a new netty immutable data buf instance. -
Method Summary
Modifier and TypeMethodDescriptionbooleanGet if the current buffer is still accessible or if it was released already.Converts this immutable buffer to a mutable one.@NonNull io.netty5.buffer.Bufferbuffer()Get the wrapped netty byte buf of this buffer, for internal use only.voidclose()Explicitly releases all data associated with this buffer making it unavailable for further reads.Disables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call torelease()orclose()is made.Enables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call torelease()orclose()is made.protected <T> TReads from this buffer, releasing it when the end of the input has been reached and releasing is enabled to prevent memory leaks.intGet the number of remaining bytes in the buffer until the buffer gets released (when enabled).booleanReads a boolean from this buffer at the current reader index.bytereadByte()Reads a byte from this buffer at the current reader index.byte[]Reads the next array of bytes from the buffer.charreadChar()Reads a 16-bit (UTF-16) char from this buffer at the current reader index.Reads the next data buf from the buffer.doubleReads a 64-bit double from this buffer at the current reader index.floatReads a 32-bit float from this buffer at the current reader index.intreadInt()Reads a 32-bit integer from this buffer at the current reader index.longreadLong()Reads a 64-bit long from this buffer at the current reader index.<T> TreadNullable(@NonNull Function<DataBuf, T> readerWhenNonNull) Reads the next requested data from the buffer.<T> TreadNullable(@NonNull Function<DataBuf, T> readerWhenNonNull, T valueWhenNull) Reads the next requested data from the buffer.<T> TreadObject(@NonNull Class<T> type) Reads the next object from the buffer at the current reader index.<T> TreadObject(@NonNull Type type) Reads the next object from the buffer at the current reader index.shortReads a 16-bit short from this buffer at the current reader index.Reads the next UTF-8 encoded string from the buffer.Reads the next unique id from the buffer at the current reader index.Redoes the currently running transaction on the buffer.voidrelease()Explicitly releases all data associated with this buffer making it unavailable for further reads.Starts a transaction to the buffer.byte[]Converts the remaining bytes in this buffer into a byte array.
-
Field Details
-
buffer
protected final io.netty5.buffer.Buffer buffer -
releasable
protected boolean releasable -
readOffset
protected volatile int readOffset -
writeOffset
protected volatile int writeOffset
-
-
Constructor Details
-
NettyImmutableDataBuf
Constructs a new netty immutable data buf instance.- Parameters:
buffer- the netty buffer to wrap.- Throws:
NullPointerException- if the given buffer is null.
-
-
Method Details
-
readBoolean
public boolean readBoolean()Reads a boolean from this buffer at the current reader index. Exactly one byte is read from the buffer.- Specified by:
readBooleanin interfaceDataBuf- Returns:
- the boolean representation of the byte at the current position.
-
readByte
public byte readByte()Reads a byte from this buffer at the current reader index. Exactly one byte is read from the buffer. -
readInt
public int readInt()Reads a 32-bit integer from this buffer at the current reader index. Exactly four bytes are read from the buffer. -
readShort
public short readShort()Reads a 16-bit short from this buffer at the current reader index. Exactly two bytes are read from the buffer. -
readLong
public long readLong()Reads a 64-bit long from this buffer at the current reader index. Exactly eight bytes are read from the buffer. -
readFloat
public float readFloat()Reads a 32-bit float from this buffer at the current reader index. Exactly four bytes are read from the buffer. -
readDouble
public double readDouble()Reads a 64-bit double from this buffer at the current reader index. Exactly eight bytes are read from the buffer.- Specified by:
readDoublein interfaceDataBuf- Returns:
- the next double in the buffer at the current reader index.
-
readChar
public char readChar()Reads a 16-bit (UTF-16) char from this buffer at the current reader index. Exactly two bytes are read from the buffer. -
readByteArray
public byte[] readByteArray()Reads the next array of bytes from the buffer. A byte array is serialized in a special way. The bytes in the buffer are prefixed with the number of bytes in the array. Two steps are made to read an array from the buffer:- The number of bytes in the following array are read from the buffer (by default a var int).
- The number of bytes the array is prefixed with are read from the buffer and put into a new array.
As the operation is dynamic there is no way to pre-calculate the amount of bytes needed to read the next byte array.
- Specified by:
readByteArrayin interfaceDataBuf- Returns:
- the next byte array in the buffer at the current reader index.
-
readUniqueId
Reads the next unique id from the buffer at the current reader index. The operation reads two longs from the buffer: the most significant bits of the unique id, and the least significant bits of the unique id. This totals to exactly sixteen bytes which are read from the buffer.- Specified by:
readUniqueIdin interfaceDataBuf- Returns:
- the next unique id in the buffer at the current reader index.
-
readString
Reads the next UTF-8 encoded string from the buffer. A string during write is converted to a byte array containing all bytes in UTF-8 form. Reading just reverses this operation. SeeDataBuf.readByteArray()for an explanation how the read operation works in detail (it's the same operation, the result is just wrapped using the string constructor).- Specified by:
readStringin interfaceDataBuf- Returns:
- the next string in the buffer at the current reader index.
-
readDataBuf
Reads the next data buf from the buffer. A data buf write works like a byte array write operation because the buffer is essentially just wrapping a byte array. SeeDataBuf.readByteArray()about the expected format.Buffers are not expected to be cross-implementation-compatible. For instance, a netty buffer can only read and write netty buffers.
- Specified by:
readDataBufin interfaceDataBuf- Returns:
- the data buf in the buffer at the current reader index.
-
toByteArray
public byte[] toByteArray()Converts the remaining bytes in this buffer into a byte array. This operation moves the reader index to the end of the buffer.- Specified by:
toByteArrayin interfaceDataBuf- Returns:
- the remaining bytes in this buffer converted to a byte array.
-
readObject
Reads the next object from the buffer at the current reader index. The object is read using the default object mapper of the system.- Specified by:
readObjectin interfaceDataBuf- Type Parameters:
T- the generic type of the object to read.- Parameters:
type- the type of the object to read.- Returns:
- the next object in the buffer at the current reader index.
- See Also:
-
readObject
Reads the next object from the buffer at the current reader index. The object is read using the default object mapper of the system.- Specified by:
readObjectin interfaceDataBuf- Type Parameters:
T- the generic type of the object to read.- Parameters:
type- the type of the object to read.- Returns:
- the next object in the buffer at the current reader index.
- See Also:
-
readNullable
Reads the next requested data from the buffer. This method call is equivalent toreadNullable(readerWhenNonNull, null).- Specified by:
readNullablein interfaceDataBuf- Type Parameters:
T- the generic type of the data to read.- Parameters:
readerWhenNonNull- the reader to read the data from the buffer when the next value is non-null.- Returns:
- the value read from the buffer or the fallback value when the buffered contained null at the position.
-
readNullable
Reads the next requested data from the buffer. To determine whether the given reader for further reading should be called, the boolean before the actual data is read. If the boolean is true, the following data is present (non-null), otherwise the given value to return when null is returned.- Specified by:
readNullablein interfaceDataBuf- Type Parameters:
T- the generic type of the data to read.- Parameters:
readerWhenNonNull- the reader to read the data from the buffer when the next value is non-null.valueWhenNull- the value to return when the buffer contains a null value at the current reader index.- Returns:
- the value read from the buffer or the fallback value when the buffered contained null at the position.
-
readableBytes
public int readableBytes()Get the number of remaining bytes in the buffer until the buffer gets released (when enabled).- Specified by:
readableBytesin interfaceDataBuf- Returns:
- the number of remaining bytes in the buffer.
-
startTransaction
Starts a transaction to the buffer. Starting a transaction while another transaction is active will override the current transaction marker. A transaction can be redone by usingDataBuf.redoTransaction().- Specified by:
startTransactionin interfaceDataBuf- Returns:
- the same instance as used to call the method, for chaining.
-
redoTransaction
Redoes the currently running transaction on the buffer. If no transaction was started before, the reader and writer index will go back to 0.- Specified by:
redoTransactionin interfaceDataBuf- Returns:
- the same instance as used to call the method, for chaining.
-
asMutable
Converts this immutable buffer to a mutable one. There is no need to copy the underlying byte tracker, meaning that all writes will be reflected into this buffer and vise-versa. -
accessible
public boolean accessible()Get if the current buffer is still accessible or if it was released already.- Specified by:
accessiblein interfaceDataBuf- Returns:
- if the current buffer is still accessible.
-
disableReleasing
Disables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call torelease()orclose()is made.- Specified by:
disableReleasingin interfaceDataBuf- Returns:
- the same instance as used to call the method, for chaining.
-
enableReleasing
Enables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call torelease()orclose()is made.- Specified by:
enableReleasingin interfaceDataBuf- Returns:
- the same instance as used to call the method, for chaining.
-
release
public void release()Explicitly releases all data associated with this buffer making it unavailable for further reads. This method does nothing if releasing was disables before calling this method. -
close
public void close()Explicitly releases all data associated with this buffer making it unavailable for further reads. This method does nothing if releasing was disables before calling this method.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceDataBuf
-
buffer
Get the wrapped netty byte buf of this buffer, for internal use only.- Returns:
- the wrapped netty byte buf.
-
hotRead
Reads from this buffer, releasing it when the end of the input has been reached and releasing is enabled to prevent memory leaks.- Type Parameters:
T- the type of data to read.- Parameters:
reader- the function which reads the requested data from the buffer.- Returns:
- the data read from the buffer.
- Throws:
NullPointerException- if the given reader function is null.
-