BufferList

public class BufferList

This class provides an implementation of a buffer that can be added to and taken from in chunks. Data is always added to the end of the buffer (using BufferList.append(...) and taken out of the buffer (using BufferList.fill(...)) from the beginning towards the end. The location indicating where the next chunk of data will be taken from is maintained, this location can then be reset to enable data to be taken out of the buffer from the beginning again.

In the below example, we create an empty BufferList instance. You can then append data to your BufferList instance, in our case writeBuffer. We then make two seperate appends. When writeBuffer contains the data which you wish to write out you can use BufferList.fill(...) to write out the data from the buffer to your chosen location, which in this case is finalArrayOfNumbers.

Usage Example:

var writeBuffer = BufferList()

let firstArrayOfNumbers: [UInt8] = [1,2,3,4]

// Append a set of data to the 'writeBuffer' object
writeBuffer.append(bytes: UnsafePointer<UInt8>(firstArrayOfNumbers),
                   length: MemoryLayout<UInt8>.size * firstArrayOfNumbers.count)

// Number of bytes stored in the 'writeBuffer' object
print(writeBuffer.count)
// Prints "4"

let secondArrayOfNumbers: [UInt8] = [5,6,7,8]

// Append a second set of data to the 'writeBuffer' object
writeBuffer.append(bytes: UnsafePointer<UInt8>(secondArrayOfNumbers),
                   length: MemoryLayout<UInt8>.size * secondArrayOfNumbers.count)

print(writeBuffer.count)
// Prints "8"

let finalArrayOfNumbers: [UInt8] = [0,0,0,0,0,0,0,0,9,10]

// Fill the destination buffer 'finalArrayOfNumbers' with the data from 'writeBuffer'
let count = writeBuffer.fill(buffer: UnsafeMutableRawPointer(mutating: finalArrayOfNumbers)
                       .assumingMemoryBound(to: UInt8.self), length: ((MemoryLayout<UInt8>.size)
                       * finalArrayOfNumbers.count))

print("count = \(count), buffer is = \(finalArrayOfNumbers)" )
// Prints "count = 8, buffer is = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"

Public

  • Get the number of bytes stored in the BufferList.

    Declaration

    Swift

    public var count: Int { get }
  • Read the data from the BufferList.

    Declaration

    Swift

    public var data: Data { get }
  • Creates a BufferList instance to store bytes to be written.

    Usage Example:

    var writeBuffer = BufferList()
    

    Declaration

    Swift

    public init()

    Return Value

    A BufferList instance.

  • Append bytes to the buffer.

    Usage Example:

    var writeBuffer = BufferList()
    let firstArrayOfNumbers: [UInt8] = [1,2,3,4]
    writeBuffer.append(bytes: UnsafePointer<UInt8>(firstArrayOfNumbers),
                       length: MemoryLayout<UInt8>.size * firstArrayOfNumbers.count)
    

    Declaration

    Swift

    public func append(bytes: UnsafePointer<UInt8>, length: Int)

    Parameters

    bytes

    The pointer to the bytes.

    length

    The number of bytes to append.

  • Append data into the BufferList.

    Usage Example:

    writeBuffer.append(data)
    

    Declaration

    Swift

    public func append(data: Data)

    Parameters

    data

    The data to append.

  • Fill an array with data from the buffer. The data is copied from the BufferList to array.

    Usage Example:

    let count = writeBuffer.fill(array: [UInt8])
    

    Declaration

    Swift

    public func fill(array: inout [UInt8]) -> Int

    Parameters

    array

    A [UInt8] for the data you want from the buffer.

    Return Value

    The number of bytes copied from this BufferList to array. This will be the length of array or the number of bytes in the buffer, whichever is smaller.

  • Fill memory with data from a BufferList. The data is copied from the BufferList to buffer.

    Usage Example:

    let count = writeBuffer.fill(buffer: UnsafeMutableRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
    

    Declaration

    Swift

    public func fill(buffer: UnsafeMutablePointer<UInt8>, length: Int) -> Int

    Parameters

    buffer

    A NSMutablePointer to the beginning of the memory to be filled.

    length

    The number of bytes to fill.

    Return Value

    The number of bytes copied from this BufferList to buffer. This will be length or the number of bytes in this BufferList, whichever is smaller.

  • Fill a Data structure with data from the buffer.

    Usage Example:

    let count = writeBuffer.fill(data: &data)
    

    Declaration

    Swift

    public func fill(data: inout Data) -> Int

    Parameters

    data

    The Data structure to fill from the data in the buffer.

    Return Value

    The number of bytes actually copied from the buffer.

  • Fill a NSMutableData with data from the buffer.

    Usage Example:

    let count = writeBuffer.fill(data: &data)
    

    Declaration

    Swift

    public func fill(data: NSMutableData) -> Int

    Parameters

    data

    The NSMutableData object to fill from the data in the buffer.

    Return Value

    The number of bytes actually copied from the buffer.

  • Reset the buffer to the beginning position and the buffer length to zero.

    Usage Example:

    writeBuffer.reset()
    

    Declaration

    Swift

    public func reset()
  • Sets the buffer back to the beginning position. The next BufferList.fill() will take data from the beginning of the buffer.

    Usage Example:

    writeBuffer.rewind()
    

    Declaration

    Swift

    public func rewind()