Home telegram - read buffer
Post
Cancel

telegram - read buffer

ReadBuffer

class ReadBuffer:MemoryBuffer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public var offset = 0

override public init(data: Data) {
    super.init(data: data)
}

public init(memory: UnsafeMutableRawPointer, length: Int, freeWhenDone: Bool) {
    super.init(memory: memory, capacity: length, length: length, freeWhenDone: freeWhenDone)
}

// init with memory buffer without copy
public init(memoryBufferNoCopy: MemoryBuffer) {
    super.init(memory: memoryBufferNoCopy.memory, capacity: memoryBufferNoCopy.length, length: memoryBufferNoCopy.length, freeWhenDone: false)
}

// [Data initWithBytesNoCopy:count]
public func dataNoCopy() -> Data {
    return Data(bytesNoCopy: self.memory.assumingMemoryBound(to: UInt8.self), count: self.length, deallocator: .none)
}

// copy self.memory.advanced(by: self.offset) to data + offset
public func read(_ data: UnsafeMutableRawPointer, offset: Int, length: Int) {
    memcpy(data + offset, self.memory.advanced(by: self.offset), length)
    self.offset += length
}

// skip length bytes
// move offset length forward
public func skip(_ length: Int) {
    self.offset += length
}

// reset offset to zero position
public func reset() {
    self.offset = 0
}

// create a new readbuffer with shared memory
public func sharedBufferNoCopy() -> ReadBuffer {
    return ReadBuffer(memory: memory, length: length, freeWhenDone: false)
}
This post is licensed under CC BY 4.0 by the author.