我正在Swift中使用InputStream。我想根据输入流的内容生成一个缓冲区(已知数据的大小很小)。但是,尝试使用getBuffer(_:length:)
会返回false
。
let sampleString = "Now is the time for all good men to come to the aid of their country"
let ssData = sampleString.data(using: .utf8)!
let ssInputStream = InputStream(data: ssData)
ssInputStream.open()
...
var buffer:UnsafeMutablePointer<UInt8>? = nil
var length:Int = 0
var success = ssInputStream.getBuffer(&buffer, length: &length)
ssInputStream.close()
// Expect
// length == 68
// success == true
// buffer != nil
// Actual
// length == 0
// success == false
// buffer == nil
我已经能够使用read
方法填充缓冲区,但是我有兴趣了解getBuffer
的问题。