mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
Add ByteBuffer.write(buffer) and set(buffer)
This commit is contained in:
1 parent
b2491dae93
commit
a7811b7b2d
2 files changed
+40
No files matched your search
@@ -70,6 +70,21 @@ extension ByteBuffer {
|
||||
return bytesRead
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func set(buffer: ByteBuffer, at index: Int) -> Int {
|
||||
return buffer.withUnsafeReadableBytes{ p in
|
||||
self.set(bytes: p, at: index)
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func write(buffer: inout ByteBuffer) -> Int {
|
||||
let written = set(buffer: buffer, at: writerIndex)
|
||||
self.moveWriterIndex(forwardBy: written)
|
||||
buffer.moveReaderIndex(forwardBy: written)
|
||||
return written
|
||||
}
|
||||
|
||||
public func slice() -> ByteBuffer {
|
||||
return slice(at: self.readerIndex, length: self.readableBytes)!
|
||||
}
|
||||
|
||||
@@ -643,4 +643,29 @@ class ByteBufferTest: XCTestCase {
|
||||
var actualGood = buf.readSlice(length: 4)
|
||||
XCTAssertEqual(Data([0, 1, 2, 3]), actualGood?.readData(length: 4))
|
||||
}
|
||||
|
||||
func testSetBuffer() throws {
|
||||
var src = try! allocator.buffer(capacity: 4)
|
||||
src.write(data: Data([0, 1, 2, 3]))
|
||||
|
||||
buf.set(buffer: src, at: 1)
|
||||
|
||||
/* Should bit increase the writerIndex of the src buffer */
|
||||
XCTAssertEqual(4, src.readableBytes)
|
||||
XCTAssertEqual(0, buf.readableBytes)
|
||||
|
||||
XCTAssertEqual(Data([0, 1, 2, 3]), buf.data(at: 1, length: 4))
|
||||
}
|
||||
|
||||
func testWriteBuffer() throws {
|
||||
var src = try! allocator.buffer(capacity: 4)
|
||||
src.write(data: Data([0, 1, 2, 3]))
|
||||
|
||||
buf.write(buffer: &src)
|
||||
|
||||
/* Should increase the writerIndex of the src buffer */
|
||||
XCTAssertEqual(0, src.readableBytes)
|
||||
XCTAssertEqual(4, buf.readableBytes)
|
||||
XCTAssertEqual(Data([0, 1, 2, 3]), buf.readData(length: 4))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user