ByteTransferStrategy
Controls how bytes are transferred between ByteBuffer
and other storage types.
enum ByteTransferStrategy
Controls how bytes are transferred between ByteBuffer
and other storage types.
enum ByteTransferStrategy
s7NIOCore10ByteBufferV19NIOFoundationCompatE0B16TransferStrategyO
What are these?8KN1
import NIOCore
The core abstractions that make up SwiftNIO.
import NIOFoundationCompat
struct ByteBuffer
ByteBuffer
stores contiguously allocated raw bytes. It is a random and sequential accessible sequence of zero or more bytes (octets).
case copy
Force a copy of the bytes.
case noCopy
Do not copy the bytes if at all possible.
case automatic
Use a heuristic to decide whether to copy the bytes or not.
init(data: Data)
func getData(at index: Int, length: Int) -> Data?
Return length
bytes starting at index
and return the result as Data
. This will not change the reader index. The selected bytes must be readable or else nil
will be returned.
func getData(at index: Int, length: Int, byteTransferStrategy: ByteBuffer.ByteTransferStrategy) -> Data?
Return length
bytes starting at index
and return the result as Data
. This will not change the reader index. The selected bytes must be readable or else nil
will be returned.
func getJSONDecodable<T>(_ type: T.Type, decoder: JSONDecoder = JSONDecoder(), at index: Int, length: Int) throws -> T? where T : Decodable
Attempts to decode the length
bytes from index
using the JSONDecoder
decoder
as T
.
func getString(at index: Int, length: Int, encoding: String.Encoding) -> String?
Get a String
decoding length
bytes starting at index
with encoding
. This will not change the reader index. The selected bytes must be readable or else nil
will be returned.
func getUUIDBytes(at index: Int) -> UUID?
Get a UUID
from the 16 bytes starting at index
. This will not change the reader index. If there are less than 16 bytes starting at index
then nil
will be returned.
func peekData(length: Int, byteTransferStrategy: ByteBuffer.ByteTransferStrategy) -> Data?
Return length
bytes starting at the current readerIndex
as Data
. This will not change the reader index.
func peekUUIDBytes() -> UUID?
Get a UUID
from the 16 bytes starting at the current readerIndex
. Does not move the reader index.
mutating func readData(length: Int) -> Data?
Read length
bytes off this ByteBuffer
, move the reader index forward by length
bytes and return the result as Data
.
mutating func readData(length: Int, byteTransferStrategy: ByteBuffer.ByteTransferStrategy) -> Data?
Read length
bytes off this ByteBuffer
, move the reader index forward by length
bytes and return the result as Data
.
mutating func readJSONDecodable<T>(_ type: T.Type, decoder: JSONDecoder = JSONDecoder(), length: Int) throws -> T? where T : Decodable
Reads length
bytes from this ByteBuffer
and then attempts to decode them using the JSONDecoder
decoder
.
mutating func readString(length: Int, encoding: String.Encoding) -> String?
Read a String
decoding length
bytes with encoding
from the readerIndex
, moving the readerIndex
appropriately.
mutating func readUUIDBytes() -> UUID?
Read a UUID
from the first 16 bytes in the buffer. Advances the reader index.
@discardableResult mutating func setContiguousBytes<Bytes>(_ bytes: Bytes, at index: Int) -> Int where Bytes : ContiguousBytes
Write bytes
into this ByteBuffer
at index
. Does not move the writer index.
@discardableResult mutating func setData<D>(_ data: D, at index: Int) -> Int where D : DataProtocol
Write the bytes of data
into this ByteBuffer
at index
. Does not move the writer index.
@discardableResult mutating func setJSONEncodable<T>(_ value: T, encoder: JSONEncoder = JSONEncoder(), at index: Int) throws -> Int where T : Encodable
Encodes value
using the JSONEncoder
encoder
and set the resulting bytes into this ByteBuffer
at the given index
.
@discardableResult mutating func setString(_ string: String, encoding: String.Encoding, at index: Int) throws -> Int
Write string
into this ByteBuffer
at index
using the encoding encoding
. Does not move the writer index.
@discardableResult mutating func setUUIDBytes(_ uuid: UUID, at index: Int) -> Int
Set the bytes of the UUID
into this ByteBuffer
at index
, allocating more storage if necessary. Does not move the writer index.
@discardableResult mutating func writeContiguousBytes<Bytes>(_ bytes: Bytes) -> Int where Bytes : ContiguousBytes
Write bytes
into this ByteBuffer
at the writer index, moving the writer index forward appropriately.
@discardableResult mutating func writeData<D>(_ data: D) -> Int where D : DataProtocol
Write the bytes of data
into this ByteBuffer
at the writer index, moving the writer index forward appropriately.
@discardableResult mutating func writeJSONEncodable<T>(_ value: T, encoder: JSONEncoder = JSONEncoder()) throws -> Int where T : Encodable
Encodes value
using the JSONEncoder
encoder
and writes the resulting bytes into this ByteBuffer
.
@discardableResult mutating func writeString(_ string: String, encoding: String.Encoding) throws -> Int
Write string
into this ByteBuffer
using the encoding encoding
, moving the writer index forward appropriately.
@discardableResult mutating func writeUUIDBytes(_ uuid: UUID) -> Int
Write a UUID
info the buffer and advances the writer index.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable : Equatable
A type that can be hashed into a Hasher
to produce an integer hash value.
protocol Sendable
A thread-safe type whose values can be shared across arbitrary concurrent contexts without introducing a risk of data races. Values of the type may have no shared mutable state, or they may protect that state with a lock or by forcing it to only be accessed from a specific actor.
static func != (lhs: Self, rhs: Self) -> Bool
Returns a Boolean value indicating whether two values are not equal.