Class BufferHolder<T extends io.netty5.util.Resource<T>>
- java.lang.Object
-
- io.netty5.buffer.api.BufferHolder<T>
-
- Type Parameters:
T- The concreteBufferHoldertype.
- All Implemented Interfaces:
io.netty5.util.Resource<T>,AutoCloseable
- Direct Known Subclasses:
BufferRef
public abstract class BufferHolder<T extends io.netty5.util.Resource<T>> extends Object implements io.netty5.util.Resource<T>
TheBufferHolderis an abstract class that simplifies the implementation of objects that themselves contain aBufferinstance.The
BufferHoldercan only hold on to a single buffer, so objects and classes that need to hold on to multiple buffers will have to do their implementation from scratch, though they can use the code of theBufferHolderas inspiration. Alternatively, multiple buffers can be composed into a single buffer, which can then be put in a buffer holder.If you just want an object that is a reference to a buffer, then the
BufferRefcan be used for that purpose. If you have an advanced use case where you wish to implementResource, and tightly control lifetimes, thenResourceSupportcan be of help.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBufferHolder(Buffer buf)Create a newBufferHolderto hold the given buffer.protectedBufferHolder(io.netty5.util.Send<Buffer> send)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()booleanequals(Object other)This implementation of theequalsoperation is restricted to work only with instances of the same class.protected BuffergetBuffer()Access the heldBufferinstance.protected BuffergetBufferVolatile()Access the heldBufferinstance.inthashCode()booleanisAccessible()protected abstract Treceive(Buffer buf)Called when a sentBufferHolderis received by the recipient.protected voidreplaceBuffer(io.netty5.util.Send<Buffer> send)Replace the underlying referenced buffer with the given buffer.protected voidreplaceBufferVolatile(io.netty5.util.Send<Buffer> send)Replace the underlying referenced buffer with the given buffer.io.netty5.util.Send<T>send()Ttouch(Object hint)
-
-
-
Constructor Detail
-
BufferHolder
protected BufferHolder(Buffer buf)
Create a newBufferHolderto hold the given buffer.- Parameters:
buf- The buffer to be held by this holder.
-
BufferHolder
protected BufferHolder(io.netty5.util.Send<Buffer> send)
Create a newBufferHolderto hold the buffer received from the givenSend.The
BufferHolderwill then be holding exclusive ownership of the buffer.- Parameters:
send- The buffer to be held by this holder.
-
-
Method Detail
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceio.netty5.util.Resource<T extends io.netty5.util.Resource<T>>
-
send
public io.netty5.util.Send<T> send()
-
receive
protected abstract T receive(Buffer buf)
Called when a sentBufferHolderis received by the recipient. TheBufferHoldershould return a new concrete instance, that wraps the givenBufferobject.- Parameters:
buf- TheBufferthat is received by the recipient, and needs to be wrapped in a newBufferHolderinstance.- Returns:
- A new buffer holder instance, containing the given buffer.
-
replaceBuffer
protected final void replaceBuffer(io.netty5.util.Send<Buffer> send)
Replace the underlying referenced buffer with the given buffer.This method is protected to permit advanced use cases of
BufferHoldersub-class implementations.Note: This method closes the current buffer, and takes exclusive ownership of the received buffer.
The buffer assignment is performed using a plain store.
- Parameters:
send- The newBufferinstance that is replacing the currently held buffer.
-
replaceBufferVolatile
protected final void replaceBufferVolatile(io.netty5.util.Send<Buffer> send)
Replace the underlying referenced buffer with the given buffer.This method is protected to permit advanced use cases of
BufferHoldersub-class implementations.Note: this method closes the current buffer, and takes exclusive ownership of the received buffer.
The buffer assignment is performed using a volatile store.
- Parameters:
send- TheSendwith the newBufferinstance that is replacing the currently held buffer.
-
getBuffer
protected final Buffer getBuffer()
Access the heldBufferinstance.The access is performed using a plain load.
- Returns:
- The
Bufferinstance being held by this buffer holder.
-
getBufferVolatile
protected final Buffer getBufferVolatile()
Access the heldBufferinstance.The access is performed using a volatile load.
- Returns:
- The
Bufferinstance being held by this buffer holder.
-
isAccessible
public boolean isAccessible()
-
equals
public boolean equals(Object other)
This implementation of theequalsoperation is restricted to work only with instances of the same class. The reason for that is that Netty library already has a number of classes that extendBufferHolderand overrideequalsmethod with an additional comparison logic, and we need the symmetric property of theequalsoperation to be preserved.
-
-