- java.lang.Object
-
- io.netty5.buffer.api.BufferHolder<T>
-
- Type Parameters:
T
- The concreteBufferHolder
type.
- All Implemented Interfaces:
Resource<T>
,AutoCloseable
- Direct Known Subclasses:
BufferRef
,DefaultHttp2GoAwayFrame
,DefaultHttp2UnknownFrame
,PemPrivateKey
,WebSocketFrame
public abstract class BufferHolder<T extends Resource<T>> extends Object implements Resource<T>
TheBufferHolder
is an abstract class that simplifies the implementation of objects that themselves contain aBuffer
instance.The
BufferHolder
can 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 theBufferHolder
as 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
BufferRef
can be used for that purpose. If you have an advanced use case where you wish to implementResource
, and tightly control lifetimes, thenResourceSupport
can be of help.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BufferHolder(Buffer buf)
Create a newBufferHolder
to hold the given buffer.protected
BufferHolder(Send<Buffer> send)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
Close the resource, making it inaccessible.boolean
equals(Object other)
This implementation of theequals
operation is restricted to work only with instances of the same class.protected Buffer
getBuffer()
Access the heldBuffer
instance.protected Buffer
getBufferVolatile()
Access the heldBuffer
instance.int
hashCode()
boolean
isAccessible()
Check if this object is accessible.protected abstract T
receive(Buffer buf)
Called when a sentBufferHolder
is received by the recipient.protected void
replaceBuffer(Send<Buffer> send)
Replace the underlying referenced buffer with the given buffer.protected void
replaceBufferVolatile(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.T
touch(Object hint)
Record the current access location for debugging purposes.
-
-
-
Constructor Detail
-
BufferHolder
protected BufferHolder(Buffer buf)
Create a newBufferHolder
to hold the given buffer.- Parameters:
buf
- The buffer to be held by this holder.
-
BufferHolder
protected BufferHolder(Send<Buffer> send)
Create a newBufferHolder
to hold the buffer received from the givenSend
.The
BufferHolder
will 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:Resource
Close the resource, making it inaccessible.Note, this method is not thread-safe unless otherwise specified.
-
send
public Send<T> send()
Description copied from interface:Resource
Send 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.
-
receive
protected abstract T receive(Buffer buf)
Called when a sentBufferHolder
is received by the recipient. TheBufferHolder
should return a new concrete instance, that wraps the givenBuffer
object.- Parameters:
buf
- TheBuffer
that is received by the recipient, and needs to be wrapped in a newBufferHolder
instance.- 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
BufferHolder
sub-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 newBuffer
instance 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
BufferHolder
sub-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 heldBuffer
instance.The access is performed using a plain load.
- Returns:
- The
Buffer
instance being held by this buffer holder.
-
getBufferVolatile
protected final Buffer getBufferVolatile()
Access the heldBuffer
instance.The access is performed using a volatile load.
- Returns:
- The
Buffer
instance being held by this buffer holder.
-
isAccessible
public boolean isAccessible()
Description copied from interface:Resource
Check if this object is accessible.
-
touch
public T touch(Object hint)
Description copied from interface:Resource
Record 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.
-
equals
public boolean equals(Object other)
This implementation of theequals
operation 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 extendBufferHolder
and overrideequals
method with an additional comparison logic, and we need the symmetric property of theequals
operation to be preserved.
-
-