Interface DataBuf.Mutable
- All Superinterfaces:
AutoCloseable,DataBuf
- Enclosing interface:
DataBuf
Represents a mutable version of a data buf.
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface eu.cloudnetservice.driver.network.buffer.DataBuf
DataBuf.Mutable -
Method Summary
Modifier and TypeMethodDescriptionadvanceWriterOffset(int delta) Advances the current writer offset of this buffer by the given delta.Wraps the underlying buffer into a read-only variant.ensureWriteable(int bytes) Ensures that this buffer has at least the given number of bytes available for writing data.intGet the remaining number of bytes available for write operations.Get a new byte buffer instance that shares the memory region of this buffer.writeBoolean(boolean b) Writes the given boolean at the current writer index, increasing the index by one.writeByte(byte b) Writes the given byte at the current writer index, increasing the index by one.writeByteArray(byte[] b) Writes the given byte array into the buffer, prefixed by an integer containing the number of bytes following in the array.writeByteArray(byte[] b, int amount) Writes the given byte array into the buffer, prefixed by an integer containing the number of bytes following in the array.writeChar(char c) Writes the given UTF-16 char at the current writer index, increasing the index by two.writeDataBuf(@NonNull DataBuf buf) Writes all data of the given data buffer into this data buffer starting at the current reader index of the given buffer.writeDouble(double d) Writes the given double at the current writer index, increasing the index by eight.writeFloat(float f) Writes the given float at the current writer index, increasing the index by four.writeInt(int integer) Writes the given integer at the current writer index, increasing the index by four.writeLong(long l) Writes the given long at the current writer index, increasing the index by eight.<T> DataBuf.MutablewriteNullable(T object, @NonNull BiConsumer<DataBuf.Mutable, T> handlerWhenNonNull) Writes the given object null-safe into this buffer.writeObject(@Nullable Object obj) Writes the given object into this buffer.intGet the current writer offset.writerOffset(int offset) Sets the current writer offset of this buffer.writeShort(short s) Writes the given short at the current writer index, increasing the index by two.writeString(@NonNull String string) Writes the string into the buffer.writeUniqueId(@NonNull UUID uuid) Writes the unique id into the buffer.Methods inherited from interface eu.cloudnetservice.driver.network.buffer.DataBuf
accessible, acquire, acquires, advanceReaderOffset, asMutable, close, forceRelease, readableBytes, readableNioBuffer, readBoolean, readByte, readByteArray, readChar, readDataBuf, readDouble, readerOffset, readerOffset, readFloat, readInt, readLong, readNullable, readNullable, readObject, readObject, readShort, readString, readUniqueId, redoTransaction, release, startTransaction, toByteArray
-
Method Details
-
writeBoolean
Writes the given boolean at the current writer index, increasing the index by one.- Parameters:
b- the boolean to write.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeInt
Writes the given integer at the current writer index, increasing the index by four.- Parameters:
integer- the integer to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeByte
Writes the given byte at the current writer index, increasing the index by one.- Parameters:
b- the byte to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeShort
Writes the given short at the current writer index, increasing the index by two.- Parameters:
s- the short to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeLong
Writes the given long at the current writer index, increasing the index by eight.- Parameters:
l- the long to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeFloat
Writes the given float at the current writer index, increasing the index by four.- Parameters:
f- the float to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeDouble
Writes the given double at the current writer index, increasing the index by eight.- Parameters:
d- the double to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeChar
Writes the given UTF-16 char at the current writer index, increasing the index by two.- Parameters:
c- the char to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeByteArray
Writes the given byte array into the buffer, prefixed by an integer containing the number of bytes following in the array.This method call is equivalent to
writeByteArray(b, b.length).- Parameters:
b- the byte array to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeByteArray
Writes the given byte array into the buffer, prefixed by an integer containing the number of bytes following in the array.- Parameters:
b- the byte array to write into the buffer.amount- the number of bytes to copy from the given byte array into this buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeUniqueId
Writes the unique id into the buffer.- Parameters:
uuid- the id to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeString
Writes the string into the buffer.- Parameters:
string- the string to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
writeDataBuf
Writes all data of the given data buffer into this data buffer starting at the current reader index of the given buffer. The reader and writer index of the given buffer are not modified by this method. The given buffer is released after being written into this buffer.Buffers are not expected to be cross-implementation-compatible. For instance, a netty buffer can only be written to netty buffers.
- Parameters:
buf- the buffer to write into this buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if either this or the given buffer were released.
-
writeObject
Writes the given object into this buffer. The object is written using the default object mapper of the system.- Parameters:
obj- the object to write into the buffer.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.- See Also:
-
writeNullable
@NonNull <T> DataBuf.Mutable writeNullable(@Nullable T object, @NonNull @NonNull BiConsumer<DataBuf.Mutable, T> handlerWhenNonNull) Writes the given object null-safe into this buffer. It appends a boolean before the actual object data to indicate if the object is non-null. The writer consumer is only called when the data is present and can then safely proceed to write all the required data into the buffer. The supplied buffer is the same buffer used for calling the method.- Type Parameters:
T- the generic type of the object being written.- Parameters:
object- the object which should be safely written into this buffer.handlerWhenNonNull- the writer of the object when it's non-null.- Returns:
- the same buffer used to call the method, for chaining.
- Throws:
IllegalStateException- if this buffer was released.
-
ensureWriteable
Ensures that this buffer has at least the given number of bytes available for writing data. If the buffer already has the number of bytes present, this method returns immediately.- Parameters:
bytes- the number of bytes that should be available for writing.- Returns:
- this buffer, for chaining.
- Throws:
IllegalArgumentException- if the given byte count is negative.IllegalStateException- if this buffer was released.
-
writeableBytes
int writeableBytes()Get the remaining number of bytes available for write operations.- Returns:
- the remaining number of bytes available for write operations.
-
writerOffset
int writerOffset()Get the current writer offset. The next write operation to this buffer will happen at the returned offset.- Returns:
- the current writer offset.
-
writerOffset
Sets the current writer offset of this buffer. The next write operation will happen from the given offset.- Parameters:
offset- the new writer offset to use.- Returns:
- this buffer, for chaining.
- Throws:
IllegalStateException- if this buffer was released.IndexOutOfBoundsException- if the given offset is beyond the end of this buffer.
-
advanceWriterOffset
Advances the current writer offset of this buffer by the given delta. The next write operation to this buffer happens at the current writer index plus the given delta. Note: the given delta cannot be negative.- Parameters:
delta- the number of bytes to move the writer index by.- Returns:
- this buffer, for chaining.
- Throws:
IllegalArgumentException- if the given delta is negative.IllegalStateException- if this buffer was released.IndexOutOfBoundsException- if advancing by the given delta would move beyond the end of this buffer.
-
writeableNioBuffer
Get a new byte buffer instance that shares the memory region of this buffer. The returned buffer can be used to read and write data to this buffer. The initial byte buffer offset is the current writer offset, and it's limited to the number of writable bytes beyond the current writer offset.Note: this api is marked as experimental as the lifecycle of the returned buffer cannot be controlled. This means that a returned byte buffer instance can still refer to memory already released by this buffer.
- Returns:
- a byte buffer sharing the memory region of this buffer, but with a separate position.
- Throws:
IllegalStateException- if this buffer was released.
-
asImmutable
-