Class BufferHolder<T extends BufferHolder<T>>
- java.lang.Object
-
- io.netty5.buffer.api.BufferHolder<T>
-
- Type Parameters:
T- The concreteBufferHoldertype.
- All Implemented Interfaces:
Resource<T>,AutoCloseable
- Direct Known Subclasses:
BufferRef
public abstract class BufferHolder<T extends BufferHolder<T>> extends Object implements 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(Send<Buffer> send)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()Close the resource, making it inaccessible.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()Check if this object is accessible.protected abstract Treceive(Buffer buf)Called when a sentBufferHolderis received by the recipient.protected voidreplaceBuffer(Send<Buffer> send)Replace the underlying referenced buffer with the given buffer.protected voidreplaceBufferVolatile(Send<Buffer> send)Replace the underlying referenced buffer with the given buffer.Send<T>send()Send this object instance to another Thread, transferring the ownership to the recipient.Ttouch(Object hint)Record the current access location for debugging purposes.
-
-
-
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(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()
Description copied from interface:ResourceClose the resource, making it inaccessible.Note, this method is not thread-safe unless otherwise specified.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceResource<T extends BufferHolder<T>>
-
send
public Send<T> send()
Description copied from interface:ResourceSend this object instance to another Thread, transferring the ownership to the recipient.The object must be in a state where it can be sent, which includes at least being accessible.
When sent, this instance will immediately become inaccessible, as if by closing it. All attempts at accessing an object that has been sent, even if that object has not yet been received, should cause an exception to be thrown.
Calling
Resource.close()on an object that has been sent will have no effect, so this method is safe to call within a try-with-resources statement.- Specified by:
sendin interfaceResource<T extends BufferHolder<T>>
-
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(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(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.
-
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()
Description copied from interface:ResourceCheck if this object is accessible.- Specified by:
isAccessiblein interfaceResource<T extends BufferHolder<T>>- Returns:
trueif this object is still valid and can be accessed, otherwisefalseif, for instance, this object has been dropped/deallocated, or been sent elsewhere.
-
touch
public T touch(Object hint)
Description copied from interface:ResourceRecord the current access location for debugging purposes. This information may be included if the resource throws a life-cycle related exception, or if it leaks. If this resource has already been closed, then this method has no effect.- Specified by:
touchin interfaceResource<T extends BufferHolder<T>>- Parameters:
hint- An optional hint about this access and its context. May benull.- Returns:
- This resource instance.
-
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.
-
-