org.springframework.core.io.buffer

Class NettyDataBufferFactory

  • java.lang.Object
    • org.springframework.core.io.buffer.NettyDataBufferFactory
  • All Implemented Interfaces:
    DataBufferFactory


    public class NettyDataBufferFactory
    extends java.lang.Object
    implements DataBufferFactory
    Implementation of the DataBufferFactory interface based on a Netty ByteBufAllocator.
    Since:
    5.0
    Author:
    Arjen Poutsma
    See Also:
    PooledByteBufAllocator, UnpooledByteBufAllocator
    • Constructor Summary

      Constructors 
      Constructor and Description
      NettyDataBufferFactory(io.netty.buffer.ByteBufAllocator byteBufAllocator)
      Creates a new NettyDataBufferFactory based on the given factory.
    • Constructor Detail

      • NettyDataBufferFactory

        public NettyDataBufferFactory(io.netty.buffer.ByteBufAllocator byteBufAllocator)
        Creates a new NettyDataBufferFactory based on the given factory.
        Parameters:
        byteBufAllocator - the factory to use
        See Also:
        PooledByteBufAllocator, UnpooledByteBufAllocator
    • Method Detail

      • getByteBufAllocator

        public io.netty.buffer.ByteBufAllocator getByteBufAllocator()
        Return the ByteBufAllocator used by this factory.
      • allocateBuffer

        public NettyDataBuffer allocateBuffer()
        Description copied from interface: DataBufferFactory
        Allocate a data buffer of a default initial capacity. Depending on the underlying implementation and its configuration, this will be heap-based or direct buffer.
        Specified by:
        allocateBuffer in interface DataBufferFactory
        Returns:
        the allocated buffer
      • allocateBuffer

        public NettyDataBuffer allocateBuffer(int initialCapacity)
        Description copied from interface: DataBufferFactory
        Allocate a data buffer of the given initial capacity. Depending on the underlying implementation and its configuration, this will be heap-based or direct buffer.
        Specified by:
        allocateBuffer in interface DataBufferFactory
        Parameters:
        initialCapacity - the initial capacity of the buffer to allocate
        Returns:
        the allocated buffer
      • wrap

        public NettyDataBuffer wrap(java.nio.ByteBuffer byteBuffer)
        Description copied from interface: DataBufferFactory
        Wrap the given ByteBuffer in a DataBuffer. Unlike allocating, wrapping does not use new memory.
        Specified by:
        wrap in interface DataBufferFactory
        Parameters:
        byteBuffer - the NIO byte buffer to wrap
        Returns:
        the wrapped buffer
      • wrap

        public DataBuffer wrap(byte[] bytes)
        Description copied from interface: DataBufferFactory
        Wrap the given byte array in a DataBuffer. Unlike allocating, wrapping does not use new memory.
        Specified by:
        wrap in interface DataBufferFactory
        Parameters:
        bytes - the byte array to wrap
        Returns:
        the wrapped buffer
      • join

        public DataBuffer join(java.util.List<? extends DataBuffer> dataBuffers)
        Return a new DataBuffer composed of the dataBuffers elements joined together. Depending on the implementation, the returned buffer may be a single buffer containing all data of the provided buffers, or it may be a true composite that contains references to the buffers.

        Note that the given data buffers do not have to be released, as they are released as part of the returned composite.

        This implementation uses Netty's CompositeByteBuf.

        Specified by:
        join in interface DataBufferFactory
        Parameters:
        dataBuffers - the data buffers to be composed
        Returns:
        a buffer that is composed from the dataBuffers argument
      • wrap

        public NettyDataBuffer wrap(io.netty.buffer.ByteBuf byteBuf)
        Wrap the given Netty ByteBuf in a NettyDataBuffer.
        Parameters:
        byteBuf - the Netty byte buffer to wrap
        Returns:
        the wrapped buffer
      • toByteBuf

        public static io.netty.buffer.ByteBuf toByteBuf(DataBuffer buffer)
        Return the given Netty DataBuffer as a ByteBuf. Returns the native buffer if buffer is a NettyDataBuffer; returns Unpooled.wrappedBuffer(ByteBuffer) otherwise.
        Parameters:
        buffer - the DataBuffer to return a ByteBuf for.
        Returns:
        the netty ByteBuf
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object