ByteBufferAllocator

public struct ByteBufferAllocator

The preferred allocator for ByteBuffer values. The allocation strategy is opaque but is currently libc’s malloc, realloc and free.

Note

ByteBufferAllocator is thread-safe.
  • Create a fresh ByteBufferAllocator. In the future the allocator might use for example allocation pools and therefore it’s recommended to reuse ByteBufferAllocators where possible instead of creating fresh ones in many places.

    Declaration

    Swift

    public init()
  • Request a freshly allocated ByteBuffer of size capacity or larger.

    Note

    The passed capacity is the ByteBuffer‘s initial capacity, it will grow automatically if necessary.

    Note

    If capacity is 0, this function will not allocate. If you want to trigger an allocation immediately, also call .clear().

    Declaration

    Swift

    public func buffer(capacity: Int) -> ByteBuffer

    Parameters

    capacity

    The initial capacity of the returned ByteBuffer.

  • Create a fresh ByteBuffer containing the bytes of the string encoded as UTF-8.

    This will allocate a new ByteBuffer with enough space to fit string and potentially some extra space.

    Declaration

    Swift

    public func buffer(string: String) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing the bytes of the string encoded as UTF-8.

    This will allocate a new ByteBuffer with enough space to fit string and potentially some extra space.

    Declaration

    Swift

    public func buffer(substring string: Substring) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing the bytes of the string encoded as UTF-8.

    This will allocate a new ByteBuffer with enough space to fit string and potentially some extra space.

    Declaration

    Swift

    public func buffer(staticString string: StaticString) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing the bytes.

    This will allocate a new ByteBuffer with enough space to fit bytes and potentially some extra space.

    Declaration

    Swift

    @inlinable
    public func buffer<Bytes>(bytes: Bytes) -> ByteBuffer where Bytes : Sequence, Bytes.Element == UInt8

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing the bytes of the byte representation in the given endianness of integer.

    This will allocate a new ByteBuffer with enough space to fit integer and potentially some extra space.

    Declaration

    Swift

    @inlinable
    public func buffer<I: FixedWidthInteger>(integer: I,
                                             endianness: Endianness = .big,
                                             as: I.Type = I.self) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing count repetitions of byte.

    This will allocate a new ByteBuffer with at least count bytes.

    Declaration

    Swift

    public func buffer(repeating byte: UInt8, count: Int) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing the readable bytes of buffer.

    This may allocate a new ByteBuffer with enough space to fit buffer and potentially some extra space.

    Note

    Use this method only if you deliberately want to reallocate a potentially smaller ByteBuffer than the one you already have. Given that ByteBuffer is a value type, defensive copies are not necessary. If you have a ByteBuffer but would like the readerIndex to start at 0, consider readSlice instead.

    Declaration

    Swift

    public func buffer(buffer: ByteBuffer) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.

  • Create a fresh ByteBuffer containing the bytes contained in the given DispatchData.

    This will allocate a new ByteBuffer with enough space to fit the bytes of the DispatchData and potentially some extra space.

    Declaration

    Swift

    public func buffer(dispatchData: DispatchData) -> ByteBuffer

    Return Value

    The ByteBuffer containing the written bytes.