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 reuseByteBufferAllocators
where possible instead of creating fresh ones in many places.Declaration
Swift
public init()
-
Request a freshly allocated
ByteBuffer
of sizecapacity
or larger.Note
The passed
capacity
is theByteBuffer
‘s initial capacity, it will grow automatically if necessary.Note
If
capacity
is0
, 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 thestring
encoded as UTF-8.This will allocate a new
ByteBuffer
with enough space to fitstring
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 thestring
encoded as UTF-8.This will allocate a new
ByteBuffer
with enough space to fitstring
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 thestring
encoded as UTF-8.This will allocate a new
ByteBuffer
with enough space to fitstring
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 thebytes
.This will allocate a new
ByteBuffer
with enough space to fitbytes
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 givenendianness
ofinteger
.This will allocate a new
ByteBuffer
with enough space to fitinteger
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
containingcount
repetitions ofbyte
.This will allocate a new
ByteBuffer
with at leastcount
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 ofbuffer
.This may allocate a new
ByteBuffer
with enough space to fitbuffer
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 thatByteBuffer
is a value type, defensive copies are not necessary. If you have aByteBuffer
but would like thereaderIndex
to start at0
, considerreadSlice
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 givenDispatchData
.This will allocate a new
ByteBuffer
with enough space to fit the bytes of theDispatchData
and potentially some extra space.Declaration
Swift
public func buffer(dispatchData: DispatchData) -> ByteBuffer
Return Value
The
ByteBuffer
containing the written bytes.