capacity
The current capacity of the storage of this ByteBuffer
, this is not constant and does not signify the number of bytes that have been written to this ByteBuffer
.
var capacity: Int { get }
The current capacity of the storage of this ByteBuffer
, this is not constant and does not signify the number of bytes that have been written to this ByteBuffer
.
var capacity: Int { get }
import NIOCore
The core abstractions that make up SwiftNIO.
struct ByteBuffer
ByteBuffer
stores contiguously allocated raw bytes. It is a random and sequential accessible sequence of zero or more bytes (octets).
@frozen struct Int
A signed integer value type.
init()
Return an empty ByteBuffer
allocated with ByteBufferAllocator()
.
init(_ view: ByteBufferView)
Create a ByteBuffer
from the given ByteBufferView
s range.
init(buffer: ByteBuffer)
Create a fresh ByteBuffer
containing the readable bytes of buffer
.
init<Bytes>(bytes: Bytes) where Bytes : Sequence, Bytes.Element == UInt8
Create a fresh ByteBuffer
containing the bytes
.
init(dispatchData: DispatchData)
Create a fresh ByteBuffer
containing the bytes contained in the given DispatchData
.
init(from decoder: Decoder) throws
Creates a ByteByffer by decoding from a Base64 encoded single value container.
init<I>(integer: I, endianness: Endianness = .big, as: I.Type = I.self) where I : FixedWidthInteger
Create a fresh ByteBuffer
containing the bytes of the byte representation in the given endianness
of integer
.
init(plainHexEncodedBytes string: String) throws
Create a fresh ByteBuffer
containing the bytes
decoded from the string representation of plainHexEncodedBytes
.
init(repeating byte: UInt8, count: Int)
Create a fresh ByteBuffer
containing count
repetitions of byte
.
init(staticString string: StaticString)
Create a fresh ByteBuffer
containing the bytes of the string
encoded as UTF-8.
init(string: String)
Create a fresh ByteBuffer
containing the bytes of the string
encoded as UTF-8.
init(substring string: Substring)
Create a fresh ByteBuffer
containing the bytes of the string
encoded as UTF-8.
var debugDescription: String { get }
var description: String { get }
A String
describing this ByteBuffer
including length and the bytes it contains (partially).
var readableBytes: Int { get }
The number of bytes readable (readableBytes
= writerIndex
- readerIndex
).
var readableBytesView: ByteBufferView { get }
A view into the readable bytes of the ByteBuffer
.
var readerIndex: Int { get }
The reader index or the number of bytes previously read from this ByteBuffer
. readerIndex
is 0
for a newly allocated ByteBuffer
.
var storageCapacity: Int { get }
The current capacity of the underlying storage of this ByteBuffer
. A COW slice of the buffer (e.g. readSlice(length: x)) will posses the same storageCapacity as the original buffer until new data is written.
var writableBytes: Int { get }
The number of bytes writable until ByteBuffer
will need to grow its underlying storage which will likely trigger a copy of the bytes.
var writerIndex: Int { get }
The write index or the number of bytes previously written to this ByteBuffer
. writerIndex
is 0
for a newly allocated ByteBuffer
.
static func == (lhs: ByteBuffer, rhs: ByteBuffer) -> Bool
Compare two ByteBuffer
values. Two ByteBuffer
values are considered equal if the readable bytes are equal.
mutating func clear()
Set both reader index and writer index to 0
. This will reset the state of this ByteBuffer
to the state of a freshly allocated one, if possible without allocations. This is the cheapest way to recycle a ByteBuffer
for a new use-case.
mutating func clear(minimumCapacity: Int)
Set both reader index and writer index to 0
. This will reset the state of this ByteBuffer
to the state of a freshly allocated one, if possible without allocations. This is the cheapest way to recycle a ByteBuffer
for a new use-case.
@discardableResult mutating func copyBytes(at fromIndex: Int, to toIndex: Int, length: Int) throws -> Int
Copies length
bytes
starting at the fromIndex
to toIndex
. Does not move the writer index.
@discardableResult mutating func discardReadBytes() -> Bool
Discard the bytes before the reader index. The byte at index readerIndex
before calling this method will be at index 0
after the call returns.
func encode(to encoder: Encoder) throws
Encodes this buffer as a base64 string in a single value container.
func getBytes(at index: Int, length: Int) -> [UInt8]?
Get length
bytes starting at index
and return the result as [UInt8]
. This will not change the reader index. The selected bytes must be readable or else nil
will be returned.
func getDispatchData(at index: Int, length: Int) -> DispatchData?
Get the bytes at index
from this ByteBuffer
as a DispatchData
. Does not move the reader index. The selected bytes must be readable or else nil
will be returned.
func getInteger<T>(at index: Int, endianness: Endianness = Endianness.big, as: T.Type = T.self) -> T? where T : FixedWidthInteger
Get the integer at index
from this ByteBuffer
. Does not move the reader index. The selected bytes must be readable or else nil
will be returned.
func getLengthPrefixedSlice<Integer>(at index: Int, endianness: Endianness = .big, as integer: Integer.Type) -> ByteBuffer? where Integer : FixedWidthInteger
Gets an Integer
from self
and gets a slice of that length from self
and returns it.
func getNullTerminatedString(at index: Int) -> String?
Get the string at index
from this ByteBuffer
decoding using the UTF-8 encoding. Does not move the reader index. The selected bytes must be readable or else nil
will be returned.
func getSlice(at index: Int, length: Int) -> ByteBuffer?
Returns a slice of size length
bytes, starting at index
. The ByteBuffer
this is invoked on and the ByteBuffer
returned will share the same underlying storage. However, the byte at index
in this ByteBuffer
will correspond to index 0
in the returned ByteBuffer
. The readerIndex
of the returned ByteBuffer
will be 0
, the writerIndex
will be length
.
func getString(at index: Int, length: Int) -> String?
Get the string at index
from this ByteBuffer
decoding using the UTF-8 encoding. Does not move the reader index. The selected bytes must be readable or else nil
will be returned.
func getUTF8ValidatedString(at index: Int, length: Int) throws -> String?
Get the string at index
from this ByteBuffer
decoding using the UTF-8 encoding. Does not move the reader index. The selected bytes must be readable or else nil
will be returned.
func hash(into hasher: inout Hasher)
The hash value for the readable bytes.
func hexDump(format: HexDumpFormat) -> String
Returns a hex dump of this ByteBuffer
in a preferred HexDumpFormat
.
mutating func modifyIfUniquelyOwned<T>(_ body: (inout ByteBuffer) throws -> T) rethrows -> T?
Modify this ByteBuffer
if this ByteBuffer
is known to uniquely own its storage.
mutating func moveReaderIndex(forwardBy offset: Int)
Move the reader index forward by offset
bytes.
mutating func moveReaderIndex(to offset: Int)
Set the reader index to offset
.
mutating func moveWriterIndex(forwardBy offset: Int)
Move the writer index forward by offset
bytes.
mutating func moveWriterIndex(to offset: Int)
Set the writer index to offset
.
mutating func readBytes(length: Int) -> [UInt8]?
Read length
bytes off this ByteBuffer
, move the reader index forward by length
bytes and return the result as [UInt8]
.
mutating func readDispatchData(length: Int) -> DispatchData?
Read length
bytes off this ByteBuffer
and return them as a DispatchData
. Move the reader index forward by length
.
mutating func readEncodedInteger<Strategy, Integer>(as: Integer.Type = Integer.self, strategy: Strategy) -> Integer? where Strategy : NIOBinaryIntegerEncodingStrategy, Integer : FixedWidthInteger
Read a binary encoded integer, moving the readerIndex
appropriately. If there are not enough bytes, nil is returned.
mutating func readInteger<T>(endianness: Endianness = .big, as: T.Type = T.self) -> T? where T : FixedWidthInteger
Read an integer off this ByteBuffer
, move the reader index forward by the integer’s byte size and return the result.
mutating func readLengthPrefixed<Integer, Result>(endianness: Endianness = .big, as integer: Integer.Type, readMessage: (ByteBuffer) throws -> Result?) throws -> Result? where Integer : FixedWidthInteger
Reads an Integer
from self
, reads a slice of that length and passes it to readMessage
. It is checked that readMessage
returns a non-nil value.
mutating func readLengthPrefixedSlice<Integer>(endianness: Endianness = .big, as integer: Integer.Type) -> ByteBuffer? where Integer : FixedWidthInteger
Reads an Integer
from self
and reads a slice of that length from self
and returns it.
mutating func readLengthPrefixedSlice<Strategy>(strategy: Strategy) -> ByteBuffer? where Strategy : NIOBinaryIntegerEncodingStrategy
Reads a slice which is prefixed with a length. The length will be read using strategy
, and then that many bytes will be read to create a slice.
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15
).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger, T13 : FixedWidthInteger, T14 : FixedWidthInteger, T15 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14
).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger, T13 : FixedWidthInteger, T14 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13
).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger, T13 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12
).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11).Type = (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10).Type = (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9).Type = (T1, T2, T3, T4, T5, T6, T7, T8, T9).self) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8).Type = (T1, T2, T3, T4, T5, T6, T7, T8).self) -> (T1, T2, T3, T4, T5, T6, T7, T8)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6, T7>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7).Type = (T1, T2, T3, T4, T5, T6, T7).self) -> (T1, T2, T3, T4, T5, T6, T7)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5, T6>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6).Type = (T1, T2, T3, T4, T5, T6).self) -> (T1, T2, T3, T4, T5, T6)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4, T5>(endianness: Endianness = .big, as: (T1, T2, T3, T4, T5).Type = (T1, T2, T3, T4, T5).self) -> (T1, T2, T3, T4, T5)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3, T4>(endianness: Endianness = .big, as: (T1, T2, T3, T4).Type = (T1, T2, T3, T4).self) -> (T1, T2, T3, T4)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2, T3>(endianness: Endianness = .big, as: (T1, T2, T3).Type = (T1, T2, T3).self) -> (T1, T2, T3)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger
mutating func readMultipleIntegers<T1, T2>(endianness: Endianness = .big, as: (T1, T2).Type = (T1, T2).self) -> (T1, T2)? where T1 : FixedWidthInteger, T2 : FixedWidthInteger
mutating func readNullTerminatedString() -> String?
Read a null terminated string off this ByteBuffer
, decoding it as String
using the UTF-8 encoding. Move the reader index forward by the string’s length and its null terminator.
mutating func readSlice(length: Int) -> ByteBuffer?
Slice length
bytes off this ByteBuffer
and move the reader index forward by length
. If enough bytes are readable the ByteBuffer
returned by this method will share the underlying storage with the ByteBuffer
the method was invoked on. The returned ByteBuffer
will contain the bytes in the range readerIndex..<(readerIndex + length)
of the original ByteBuffer
. The readerIndex
of the returned ByteBuffer
will be 0
, the writerIndex
will be length
.
mutating func readString(length: Int) -> String?
Read length
bytes off this ByteBuffer
, decoding it as String
using the UTF-8 encoding. Move the reader index forward by length
.
mutating func readUTF8ValidatedString(length: Int) throws -> String?
Read length
bytes off this ByteBuffer
, decoding it as String
using the UTF-8 encoding. Move the reader index forward by length
.
@discardableResult mutating func readWithUnsafeMutableReadableBytes(_ body: (UnsafeMutableRawBufferPointer) throws -> Int) rethrows -> Int
Yields a mutable buffer pointer containing this ByteBuffer
’s readable bytes. You may modify the yielded bytes. Will move the reader index by the number of bytes returned by body
but leave writer index as it was.
mutating func readWithUnsafeMutableReadableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> (Int, T)) rethrows -> T
Yields a mutable buffer pointer containing this ByteBuffer
’s readable bytes. You may modify the yielded bytes. Will move the reader index by the number of bytes body
returns in the first tuple component but leave writer index as it was.
@discardableResult mutating func readWithUnsafeReadableBytes(_ body: (UnsafeRawBufferPointer) throws -> Int) rethrows -> Int
Yields an immutable buffer pointer containing this ByteBuffer
’s readable bytes. Will move the reader index by the number of bytes returned by body
.
mutating func readWithUnsafeReadableBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> (Int, T)) rethrows -> T
Yields an immutable buffer pointer containing this ByteBuffer
’s readable bytes. Will move the reader index by the number of bytes body
returns in the first tuple component.
mutating func reserveCapacity(_ minimumCapacity: Int)
Reserves enough space to store the specified number of bytes.
mutating func reserveCapacity(minimumWritableBytes: Int)
Reserves enough space to write at least the specified number of bytes.
@discardableResult mutating func setBuffer(_ buffer: ByteBuffer, at index: Int) -> Int
Copy buffer
’s readable bytes into this ByteBuffer
starting at index
. Does not move any of the reader or writer indices.
@discardableResult mutating func setBytes(_ bytes: UnsafeRawBufferPointer, at index: Int) -> Int
Copy bytes
into the ByteBuffer
at index
. Does not move the writer index.
@discardableResult mutating func setBytes<Bytes>(_ bytes: Bytes, at index: Int) -> Int where Bytes : Sequence, Bytes.Element == UInt8
Copy the collection of bytes
into the ByteBuffer
at index
. Does not move the writer index.
@discardableResult mutating func setDispatchData(_ dispatchData: DispatchData, at index: Int) -> Int
Write dispatchData
into this ByteBuffer
at index
. Does not move the writer index.
@discardableResult mutating func setInteger<T>(_ integer: T, at index: Int, endianness: Endianness = .big, as: T.Type = T.self) -> Int where T : FixedWidthInteger
Write integer
into this ByteBuffer
starting at index
. This does not alter the writer index.
mutating func setNullTerminatedString(_ string: String, at index: Int) -> Int
Write string
null terminated into this ByteBuffer
at index
using UTF-8 encoding. Does not move the writer index.
@discardableResult mutating func setRepeatingByte(_ byte: UInt8, count: Int, at index: Int) -> Int
Sets the given byte
count
times at the given index
. Will reserve more memory if necessary. Does not move the writer index.
mutating func setStaticString(_ string: StaticString, at index: Int) -> Int
Write the static string
into this ByteBuffer
at index
using UTF-8 encoding, moving the writer index forward appropriately.
@discardableResult mutating func setString(_ string: String, at index: Int) -> Int
Write string
into this ByteBuffer
at index
using UTF-8 encoding. Does not move the writer index.
@discardableResult mutating func setSubstring(_ substring: Substring, at index: Int) -> Int
Write substring
into this ByteBuffer
at index
using UTF-8 encoding. Does not move the writer index.
@discardableResult mutating func shrinkBufferCapacity(to desiredCapacity: Int) -> Bool
The ByteBuffer
will successfully be shrunk if the requested capacity is less than the current capacity, and the requested capacity is greater than or equal to the number of readable bytes in the buffer. If either condition is not true, the buffer will not be shrunk.
func slice() -> ByteBuffer
Slice the readable bytes off this ByteBuffer
without modifying the reader index. This method will return a ByteBuffer
sharing the underlying storage with the ByteBuffer
the method was invoked on. The returned ByteBuffer
will contain the bytes in the range readerIndex..<writerIndex
of the original ByteBuffer
.
func viewBytes(at index: Int, length: Int) -> ByteBufferView?
Returns a view into some portion of the readable bytes of a ByteBuffer
.
mutating func withUnsafeMutableReadableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T
Yields a mutable buffer pointer containing this ByteBuffer
’s readable bytes. You may modify those bytes.
mutating func withUnsafeMutableWritableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T
Yields the bytes currently writable (bytesWritable
= capacity
- writerIndex
). Before reading those bytes you must first write to them otherwise you will trigger undefined behaviour. The writer index will remain unchanged.
func withUnsafeReadableBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
Yields a buffer pointer containing this ByteBuffer
’s readable bytes.
func withUnsafeReadableBytesWithStorageManagement<T>(_ body: (UnsafeRawBufferPointer, Unmanaged<AnyObject>) throws -> T) rethrows -> T
Yields a buffer pointer containing this ByteBuffer
‘s readable bytes. You may hold a pointer to those bytes even after the closure returned iff you model the lifetime of those bytes correctly using the Unmanaged
instance. If you don’t require the pointer after the closure returns, use withUnsafeReadableBytes
.
func withVeryUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
This vends a pointer to the storage of the ByteBuffer
. It’s marked as very unsafe because it might contain uninitialised memory and it’s undefined behaviour to read it. In most cases you should use withUnsafeReadableBytes
.
func withVeryUnsafeBytesWithStorageManagement<T>(_ body: (UnsafeRawBufferPointer, Unmanaged<AnyObject>) throws -> T) rethrows -> T
See withUnsafeReadableBytesWithStorageManagement
and withVeryUnsafeBytes
.
mutating func withVeryUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T
This vends a pointer to the storage of the ByteBuffer
. It’s marked as very unsafe because it might contain uninitialised memory and it’s undefined behaviour to read it. In most cases you should use withUnsafeMutableWritableBytes
.
@discardableResult mutating func writeBuffer(_ buffer: inout ByteBuffer) -> Int
Write buffer
’s readable bytes into this ByteBuffer
starting at writerIndex
. This will move both this ByteBuffer
’s writer index as well as buffer
’s reader index by the number of bytes readable in buffer
.
@discardableResult mutating func writeBytes(_ bytes: UnsafeRawBufferPointer) -> Int
Write bytes
into this ByteBuffer
. Moves the writer index forward by the number of bytes written.
@discardableResult mutating func writeBytes<Bytes>(_ bytes: Bytes) -> Int where Bytes : Sequence, Bytes.Element == UInt8
Write bytes
, a Sequence
of UInt8
into this ByteBuffer
. Moves the writer index forward by the number of bytes written.
@discardableResult mutating func writeDispatchData(_ dispatchData: DispatchData) -> Int
Write dispatchData
into this ByteBuffer
, moving the writer index forward appropriately.
@discardableResult mutating func writeEncodedInteger<Integer, Strategy>(_ value: Integer, strategy: Strategy) -> Int where Integer : FixedWidthInteger, Strategy : NIOBinaryIntegerEncodingStrategy
Write a binary encoded integer.
@discardableResult mutating func writeImmutableBuffer(_ buffer: ByteBuffer) -> Int
@discardableResult mutating func writeInteger<T>(_ integer: T, endianness: Endianness = .big, as: T.Type = T.self) -> Int where T : FixedWidthInteger
Write integer
into this ByteBuffer
, moving the writer index forward appropriately.
@discardableResult mutating func writeLengthPrefixed<Integer>(endianness: Endianness = .big, as integer: Integer.Type, writeMessage: (inout ByteBuffer) throws -> Int) throws -> Int where Integer : FixedWidthInteger
Prefixes a message written by writeMessage
with the number of bytes written as an Integer
.
@discardableResult mutating func writeLengthPrefixed<Strategy>(strategy: Strategy, writeData: (_ buffer: inout ByteBuffer) throws -> Int) rethrows -> Int where Strategy : NIOBinaryIntegerEncodingStrategy
Prefixes bytes written by writeData
with the number of bytes written. The number of bytes written is encoded using strategy
.
@discardableResult mutating func writeLengthPrefixedBuffer<Strategy>(_ buffer: ByteBuffer, strategy: Strategy) -> Int where Strategy : NIOBinaryIntegerEncodingStrategy
Write the length of buffer
using strategy
. Then write the buffer.
@discardableResult mutating func writeLengthPrefixedBytes<Bytes, Strategy>(_ bytes: Bytes, strategy: Strategy) -> Int where Bytes : Sequence, Strategy : NIOBinaryIntegerEncodingStrategy, Bytes.Element == UInt8
Write the length of bytes
using strategy
. Then write the bytes.
@discardableResult mutating func writeLengthPrefixedString<Strategy>(_ string: String, strategy: Strategy) -> Int where Strategy : NIOBinaryIntegerEncodingStrategy
Write the length of string
using strategy
. Then write the string.
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, _ value10: T10, _ value11: T11, _ value12: T12, _ value13: T13, _ value14: T14, _ value15: T15, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15
).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger, T13 : FixedWidthInteger, T14 : FixedWidthInteger, T15 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, _ value10: T10, _ value11: T11, _ value12: T12, _ value13: T13, _ value14: T14, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14
).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger, T13 : FixedWidthInteger, T14 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, _ value10: T10, _ value11: T11, _ value12: T12, _ value13: T13, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13
).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger, T13 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, _ value10: T10, _ value11: T11, _ value12: T12, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12).Type = (
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12
).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger, T12 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, _ value10: T10, _ value11: T11, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11).Type = (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger, T11 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, _ value10: T10, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10).Type = (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger, T10 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8, T9>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, _ value9: T9, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8, T9).Type = (T1, T2, T3, T4, T5, T6, T7, T8, T9).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger, T9 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7, T8>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, _ value8: T8, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7, T8).Type = (T1, T2, T3, T4, T5, T6, T7, T8).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger, T8 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6, T7>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, _ value7: T7, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6, T7).Type = (T1, T2, T3, T4, T5, T6, T7).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger, T7 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5, T6>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, _ value6: T6, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5, T6).Type = (T1, T2, T3, T4, T5, T6).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger, T6 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4, T5>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, _ value5: T5, endianness: Endianness = .big, as: (T1, T2, T3, T4, T5).Type = (T1, T2, T3, T4, T5).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger, T5 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3, T4>(_ value1: T1, _ value2: T2, _ value3: T3, _ value4: T4, endianness: Endianness = .big, as: (T1, T2, T3, T4).Type = (T1, T2, T3, T4).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger, T4 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2, T3>(_ value1: T1, _ value2: T2, _ value3: T3, endianness: Endianness = .big, as: (T1, T2, T3).Type = (T1, T2, T3).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger, T3 : FixedWidthInteger
@discardableResult mutating func writeMultipleIntegers<T1, T2>(_ value1: T1, _ value2: T2, endianness: Endianness = .big, as: (T1, T2).Type = (T1, T2).self) -> Int where T1 : FixedWidthInteger, T2 : FixedWidthInteger
@discardableResult mutating func writeNullTerminatedString(_ string: String) -> Int
Write string
into this ByteBuffer
null terminated using UTF-8 encoding, moving the writer index forward appropriately.
@discardableResult mutating func writePlainHexEncodedBytes(_ plainHexEncodedBytes: String) throws -> Int
Write ASCII hexadecimal string
into this ByteBuffer
as raw bytes, decoding the hexadecimal & moving the writer index forward appropriately. This method will throw if the string input is not of the “plain” hex encoded format.
@discardableResult mutating func writeRepeatingByte(_ byte: UInt8, count: Int) -> Int
Writes byte
count
times. Moves the writer index forward by the number of bytes written.
@discardableResult mutating func writeStaticString(_ string: StaticString) -> Int
Write the static string
into this ByteBuffer
using UTF-8 encoding, moving the writer index forward appropriately.
@discardableResult mutating func writeString(_ string: String) -> Int
Write string
into this ByteBuffer
using UTF-8 encoding, moving the writer index forward appropriately.
@discardableResult mutating func writeSubstring(_ substring: Substring) -> Int
Write substring
into this ByteBuffer
using UTF-8 encoding, moving the writer index forward appropriately.
@discardableResult mutating func writeWithUnsafeMutableBytes(minimumWritableBytes: Int, _ body: (UnsafeMutableRawBufferPointer) throws -> Int) rethrows -> Int
This vends a pointer of the ByteBuffer
at the writerIndex
after ensuring that the buffer has at least minimumWritableBytes
of writable bytes available.
struct CopyBytesError
Errors thrown when calling copyBytes
.
struct HexDecodingError
An error that is thrown when an invalid hex encoded string was attempted to be written to a ByteBuffer.
struct HexDumpFormat
Describes a ByteBuffer hexDump format. Can be either xxd output compatible, or hexdump compatible.
struct LengthPrefixError
struct QUICBinaryEncodingStrategy
A NIOBinaryIntegerEncodingStrategy
which encodes bytes as defined in RFC 9000 § 16
struct ReadUTF8ValidationError
Errors thrown when calling readUTF8ValidatedString
or getUTF8ValidatedString
.
typealias _Capacity = UInt32
typealias _Index = UInt32
mutating func clear(minimumCapacity: UInt32)
Set both reader index and writer index to 0
. This will reset the state of this ByteBuffer
to the state of a freshly allocated one, if possible without allocations. This is the cheapest way to recycle a ByteBuffer
for a new use-case.
@discardableResult mutating func set(buffer: ByteBuffer, at index: Int) -> Int
Copy buffer
’s readable bytes into this ByteBuffer
starting at index
. Does not move any of the reader or writer indices.
@discardableResult mutating func writeWithUnsafeMutableBytes(_ body: (UnsafeMutableRawBufferPointer) throws -> Int) rethrows -> Int