Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OutputStream将循环缓冲(swift nio)写入swift中的文件?_Swift_Circular Buffer_Swift Nio - Fatal编程技术网

使用OutputStream将循环缓冲(swift nio)写入swift中的文件?

使用OutputStream将循环缓冲(swift nio)写入swift中的文件?,swift,circular-buffer,swift-nio,Swift,Circular Buffer,Swift Nio,我正试图使用SwiftNIO的循环缓冲区来存储数据,一旦缓冲区几乎满了,就使用输出流将内容转储到文件中。不幸的是,OutputStream.write()方法将UnsafePointer作为参数,而CircularBuffer可以输出UnsafeBufferPointer。有没有办法将循环缓冲转换为非安全指针 我尝试使用以下代码扩展CircularBuffer,我正在使用这些代码成功地将结构转换为字节数组,因为有人认为CircularBuffer实际上是一个结构,但我的输出文件中有垃圾: ext

我正试图使用SwiftNIO的
循环缓冲区来存储数据,一旦缓冲区几乎满了,就使用
输出流将内容转储到文件中。不幸的是,
OutputStream.write()
方法将
UnsafePointer
作为参数,而
CircularBuffer
可以输出
UnsafeBufferPointer
。有没有办法将
循环缓冲
转换为
非安全指针

我尝试使用以下代码扩展CircularBuffer,我正在使用这些代码成功地将结构转换为字节数组,因为有人认为CircularBuffer实际上是一个结构,但我的输出文件中有垃圾:

extension CircularBuffer {
    func toBytes() -> [UInt8] {
        let capacity = MemoryLayout<Self>.size
        var mutableValue = self
        return withUnsafePointer(to: &mutableValue) {
            return $0.withMemoryRebound(to: UInt8.self, capacity: capacity) {
                return Array(UnsafeBufferPointer(start: $0, count: capacity))
            }
        }
    }
}
分机循环缓冲{
函数toBytes()->[UInt8]{
让容量=MemoryLayout.size
变量可变值=自身
返回withUnsafePointer(指向:&mutableValue){
返回$0.withMemoryRebound(收件人:UInt8.self,容量:容量){
返回数组(UnsafeBufferPointer(开始:$0,计数:容量))
}
}
}
}
有什么想法吗?

是一个结构,具有用于元素存储的内部
连续数组
ContiguousArray
也是一个结构,具有指向实际元素存储的内部指针

当前代码产生垃圾是因为它返回
struct CircularBuffer
本身的内存表示,而不是它所表示的元素的字节

作为一个集合,
CircularBuffer
有一个
WithContinguousStorageIFAvailable()
方法,如果存在元素存储,该方法使用指向元素存储的指针调用闭包。使用
UnsafeBufferPointer
参数调用闭包,您可以从中获得
baseAddress

var buffer: CircularBuffer<UInt8> = ...
let os: OutputStream = ...
// ...
let amountWritten =  buffer.withContiguousStorageIfAvailable {
    os.write($0.baseAddress!, maxLength: $0.count)
}
是具有用于元素存储的内部
连续数组
的结构
ContiguousArray
也是一个结构,具有指向实际元素存储的内部指针

当前代码产生垃圾是因为它返回
struct CircularBuffer
本身的内存表示,而不是它所表示的元素的字节

作为一个集合,
CircularBuffer
有一个
WithContinguousStorageIFAvailable()
方法,如果存在元素存储,该方法使用指向元素存储的指针调用闭包。使用
UnsafeBufferPointer
参数调用闭包,您可以从中获得
baseAddress

var buffer: CircularBuffer<UInt8> = ...
let os: OutputStream = ...
// ...
let amountWritten =  buffer.withContiguousStorageIfAvailable {
    os.write($0.baseAddress!, maxLength: $0.count)
}

您还可以一次循环一个缓冲区的元素,尽管这可能会非常低效:

    var cb = CircularBuffer<UInt8>()
    cb.append(contentsOf:[1,2,3])
    cb.append(contentsOf:[4,5,6])
    let stream = OutputStream.toMemory()
    stream.open()
    for var i in cb {
        stream.write(&i, maxLength:1)
        print(i)
    }
    stream.close()
    print(stream.property(forKey: .dataWrittenToMemoryStreamKey) as Any)
var cb=CircularBuffer()
cb.追加(内容:[1,2,3])
cb.追加(内容:[4,5,6])
let stream=OutputStream.toMemory()
stream.open()
对于cb中的var i{
stream.write(&i,maxLength:1)
印刷品(一)
}
stream.close()
打印(stream.property(forKey:.datawritentomerystreamkey)如有)

您也可以一次循环一个缓冲区的元素,尽管这可能会非常低效:

    var cb = CircularBuffer<UInt8>()
    cb.append(contentsOf:[1,2,3])
    cb.append(contentsOf:[4,5,6])
    let stream = OutputStream.toMemory()
    stream.open()
    for var i in cb {
        stream.write(&i, maxLength:1)
        print(i)
    }
    stream.close()
    print(stream.property(forKey: .dataWrittenToMemoryStreamKey) as Any)
var cb=CircularBuffer()
cb.追加(内容:[1,2,3])
cb.追加(内容:[4,5,6])
let stream=OutputStream.toMemory()
stream.open()
对于cb中的var i{
stream.write(&i,maxLength:1)
印刷品(一)
}
stream.close()
打印(stream.property(forKey:.datawritentomerystreamkey)如有)

如果您有一个UnsafeBufferPointer,那么您可以获取它的
.baseAddress
来获取UnsafePointer。是否重复?你不能通过再次问同一个问题来避免重复。对不起,马特认为它已经被删除了。虽然我同意应该改进一个问题(而不是删除并再次问),但我也认为另一个问题被错误地作为重复问题关闭了。这不是关于将结构写入文件,而是关于将循环缓冲区结构的“内容”写入文件。如果您有一个UnsafeBufferPointer,那么您可以使用它的
.baseAddress
获取一个UnsafePointer。是否重复?你不能通过再次问同一个问题来避免重复。对不起,马特认为它已经被删除了。虽然我同意应该改进一个问题(而不是删除并再次问),但我也认为另一个问题被错误地作为重复问题关闭了。这不是将结构写入文件,而是将循环缓冲区结构的“内容”写入文件。因此,目前,除了将内容复制到数组中,然后将其输出外,没有其他选择:(谢谢您的回复。@jiko:内部存储是一个
元素的数组?
,也就是可选的数组,因此实际上没有指向
元素
s的连续存储的指针,可以传递给
写入
方法(这在我链接的GitHub问题中讨论)。因此,您必须以某种方式提取元素。要么将所有元素一起作为一个数组,要么按照Matt的建议逐个提取。因此,目前,除了将内容复制到一个数组中,然后将其输出外,别无选择:(谢谢您的回复。@jiko:内部存储是一个
元素的数组?
,也就是可选的数组,因此实际上没有指向
元素
s的连续存储的指针,可以传递给
写入
方法(这在我链接的GitHub问题中讨论).所以你必须以某种方式提取元素。要么全部作为一个数组,要么按照Matt的建议逐个提取。