RingByteBuffer

open class RingByteBuffer

This class is a simple wrapper around a Data object that allows for treating the data like a stream of bytes. The three basic operations are get(buffer:maxLength:), append(buffer:length:), and prepend(buffer:length:).

NOTE: This class is NOT thread safe.

  • The number of bytes in the available.

    Declaration

    Swift

    public final var count: Int { get }
  • Returns true if there are bytes in the ring buffer.

    Declaration

    Swift

    public final var hasBytesAvailable: Bool { get }
  • The total capacity of the buffer.

    Declaration

    Swift

    public final var capacity: Int { get }
  • The room in the buffer for new bytes.

    Declaration

    Swift

    public final var headroom: Int { get }
  • Returns true if the buffer is empty.

    Declaration

    Swift

    public final var isEmpty: Bool { get }
  • Creates a new instance of ByteBuffer.

    Declaration

    Swift

    public init(initialCapacity: Int = BasicBufferSize)

    Parameters

    initialCapacity

    the initial capacity of the buffer.

  • Clear the buffer.

    Declaration

    Swift

    public final func clear(keepingCapacity: Bool)

    Parameters

    keepingCapacity

    true if the capacity should be retained. false if the buffer should shrink to it’s original size.

  • Read a byte from the buffer.

    Declaration

    Swift

    public final func get(byte: inout UInt8) -> Bool

    Parameters

    byte

    a pointer to the variable to receive the byte.

    Return Value

    true if the byte was retrived or false if the buffer was empty.

  • Read a byte from the buffer.

    Declaration

    Swift

    public final func get() -> UInt8?

    Return Value

    The next byte or nil if the buffer is empty.

  • Get, at most, the first maxLength bytes from the buffer.

    Declaration

    Swift

    public final func get(dest: BytePointer, maxLength: Int) -> Int

    Parameters

    dest

    The destination buffer.

    maxLength

    The maximum number of bytes the destination buffer can hold.

    Return Value

    The number of bytes actually gotten.

  • Get bytes from the buffer into the instance of EasyByteBuffer.

    Declaration

    Swift

    public final func get(dest: EasyByteBuffer) -> Int

    Parameters

    dest

    The destination buffer.

    Return Value

    The number of bytes actually gotten.

  • Get the bytes from the buffer.

    Declaration

    Swift

    public final func get(dest: inout Data, maxLength: Int = -1, overWrite: Bool = true) -> Int

    Parameters

    destData

    The instance of Data that will receive the bytes.

    maxLength

    The maximum number of bytes to get. If -1 then all available bytes will be fetched.

    overWrite

    true if destData should be cleared of any existing bytes first.

    Return Value

    The number of bytes added to destData.

  • Get bytes from the END of the buffer.

    Declaration

    Swift

    public final func getFromEnd(dest: UnsafeMutableRawPointer, maxLength: Int) -> Int

    Parameters

    dest

    The destination buffer.

    maxLength

    The maximum number of bytes to read.

    Return Value

    The number of bytes actually read.

  • Append the given RingByteBuffer to this RingByteBuffer.

    Declaration

    Swift

    public final func append(src: RingByteBuffer)

    Parameters

    ringBuffer

    the source ring buffer.

  • Append the given byte to the buffer.

    Declaration

    Swift

    public final func append(byte: UInt8)

    Parameters

    byte

    the byte to append.

  • Append the given bytes to the buffer.

    Declaration

    Swift

    public final func append<T>(src: UnsafePointer<T>, length: Int)

    Parameters

    src

    The source buffer.

    length

    The number of bytes in the source buffer.

  • Append the given bytes to the buffer.

    Declaration

    Swift

    public final func append(src: EasyByteBuffer, reset: Bool = true)

    Parameters

    ezBuffer

    The buffer to append bytes from.

  • Append the given bytes to the buffer.

    Declaration

    Swift

    public final func append(src: UnsafeRawPointer, length: Int, maxLength: Int = Int.max)

    Parameters

    rawSrc

    The source buffer.

    length

    The number of bytes in the source buffer.

  • Append the given bytes to the buffer.

    Declaration

    Swift

    public final func append<T>(src: UnsafeBufferPointer<T>)

    Parameters

    buffSrc

    the source buffer.

  • Append the given bytes to the buffer.

    Declaration

    Swift

    public final func append(src: UnsafeRawBufferPointer)

    Parameters

    rawBuffSrc

    the source buffer.

  • Append the given bytes to the buffer.

    Declaration

    Swift

    public final func append(src: Data)

    Parameters

    dataSrc

    the source data.

  • Prepend the given byte to the buffer. This byte will be prepended so that it is the next byte that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend(byte: UInt8)

    Parameters

    byte

    The byte to prepend.

  • Prepend the given RingByteBuffer to this RingByteBuffer.

    Declaration

    Swift

    public final func prepend(src: RingByteBuffer)

    Parameters

    ringBuffer

    the source ring buffer.

  • Prepend the given bytes to the buffer. These bytes will be prepended so that they are the next bytes that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend(src: UnsafeRawPointer, length: Int)

    Parameters

    rawSrc

    The source buffer.

    length

    The number of bytes in the source buffer.

  • Prepend the given bytes to the buffer. These bytes will be prepended so that they are the next bytes that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend<T>(src: UnsafePointer<T>, length: Int)

    Parameters

    src

    The source buffer.

    length

    The number of bytes in the source buffer.

  • Prepend the given bytes to the buffer. These bytes will be prepended so that they are the next bytes that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend(src: UnsafeRawBufferPointer)

    Parameters

    rawBuffSrc

    the source buffer.

  • Prepend the given bytes to the buffer. These bytes will be prepended so that they are the next bytes that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend<T>(src: UnsafeBufferPointer<T>)

    Parameters

    buffSrc

    the source buffer.

  • Prepend the given bytes to the buffer. These bytes will be prepended so that they are the next bytes that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend(src: Data)

    Parameters

    srcData

    the source data.

  • Prepend the given bytes to the buffer. These bytes will be prepended so that they are the next bytes that will be read via get(buffer:maxLength:).

    Declaration

    Swift

    public final func prepend(src b: EasyByteBuffer, reset: Bool = true)

    Parameters

    ezBuffer

    the source data.

  • Get’s the data one contiguous range at a time.

    Throws

    Any error thrown by the closure.

    Declaration

    Swift

    public final func forEachContiguousRange(_ body: (ByteROPointer, Int) throws -> Int) throws -> Int

    Parameters

    body

    the closure to call for each block of data.

    Return Value

    The bytes read.

  • Swap every 2 bytes of data.

    Declaration

    Swift

    public final func swapEndian16() -> Int

    Return Value

    The number of bytes swapped.

  • Swap every 4 bytes of data.

    Declaration

    Swift

    public final func swapEndian32() -> Int

    Return Value

    The number of bytes swapped.

  • Swap every 8 bytes of data.

    Declaration

    Swift

    public final func swapEndian64() -> Int

    Return Value

    The number of bytes swapped.