diff --git a/Sources/NIO/ByteBuffer.swift b/Sources/NIO/ByteBuffer.swift index 41f63623f..b913f2da0 100644 --- a/Sources/NIO/ByteBuffer.swift +++ b/Sources/NIO/ByteBuffer.swift @@ -88,12 +88,9 @@ public struct ByteBuffer { // TODO: Equatable, Comparable case maxCapacityExceeded } - private var data: Data - - public var capacity: Int { - return self.data.count - } - + // Mark as internal so we can access it in tests. + internal var data: Data + /** Adjusts the capacity of the buffer. If the new capacity is less than the current capacity, the content of this buffer is truncated. If the new capacity is greater than the @@ -119,6 +116,9 @@ public struct ByteBuffer { // TODO: Equatable, Comparable public private(set) var allocator: ByteBufferAllocator + private let offset: Int + public private(set) var capacity: Int + public private(set) var readerIndex = 0, markedReaderIndex = 0 public private(set) var writerIndex = 0, markedWriterIndex = 0 @@ -132,8 +132,19 @@ public struct ByteBuffer { // TODO: Equatable, Comparable self.allocator = allocator self.maxCapacity = maxCapacity self.data = ByteBuffer.reallocatedData(minimumCapacity: startingCapacity, source: nil, allocator: allocator) + self.offset = 0 + self.capacity = data.count } + private init(allocator: ByteBufferAllocator, data: Data, offset: Int, length: Int, maxCapacity: Int) { + self.allocator = allocator + self.maxCapacity = maxCapacity + self.writerIndex = length + self.offset = offset + self.data = data + self.capacity = length + } + /** Discards the bytes between the 0th index and readerIndex. It moves the bytes between readerIndex and writerIndex to the 0th index, and sets readerIndex to 0 and writerIndex to oldWriterIndex - oldReaderIndex @@ -164,22 +175,28 @@ public struct ByteBuffer { // TODO: Equatable, Comparable self.data = ByteBuffer.reallocatedData(minimumCapacity: capacity + deficit, source: self.data, allocator: self.allocator) + self.capacity = data.count return (enoughSpace: true, capacityIncreased: true) } + + private func applyOffset(_ index: Int) -> Int { + return index + offset + } + public func withReadPointer(body: (UnsafePointer, Int) throws -> T) rethrows -> T { - return try data.withUnsafeBytes({ try body($0.advanced(by: readerIndex), readableBytes) }) + return try data.withUnsafeBytes({ try body($0.advanced(by: applyOffset(readerIndex)), readableBytes) }) } public func withWritePointer(body: (UnsafePointer, Int) throws -> T) rethrows -> T { - return try data.withUnsafeBytes({ try body($0.advanced(by: writerIndex), writableBytes) }) + return try data.withUnsafeBytes({ try body($0.advanced(by: applyOffset(writerIndex)), writableBytes) }) } // Mutable versions for writing to the buffer. body function returns the number of bytes written and writerIndex // will be automatically moved. public mutating func withMutableWritePointer(body: (UnsafeMutablePointer, Int) throws -> Int?) rethrows -> Int? { - let bytesWritten = try data.withUnsafeMutableBytes({ return try body($0.advanced(by: writerIndex), writableBytes) }) + let bytesWritten = try data.withUnsafeMutableBytes({ return try body($0.advanced(by: applyOffset(writerIndex)), writableBytes) }) advanceWriterIndex(bytesWritten ?? 0) @@ -187,7 +204,7 @@ public struct ByteBuffer { // TODO: Equatable, Comparable } public mutating func withMutableReadPointer(body: (UnsafeMutablePointer, Int) throws -> Int?) rethrows -> Int? { - let bytesWritten = try data.withUnsafeMutableBytes { try body($0.advanced(by: readerIndex), readableBytes) } + let bytesWritten = try data.withUnsafeMutableBytes { try body($0.advanced(by: applyOffset(readerIndex)), readableBytes) } advanceReaderIndex(bytesWritten ?? 0) @@ -220,7 +237,7 @@ public struct ByteBuffer { // TODO: Equatable, Comparable } public mutating func readInteger(endianess: Endianess = .Big) -> T? { - if let int: T = getInteger(index: readerIndex, endianess: endianess) { + if let int: T = getInteger0(index: applyOffset(readerIndex), limit: applyOffset(writerIndex), endianess: endianess) { readerIndex += MemoryLayout.size return int } @@ -228,7 +245,11 @@ public struct ByteBuffer { // TODO: Equatable, Comparable } public func getInteger(index: Int, endianess: Endianess = .Big) -> T? { - guard index + MemoryLayout.size < capacity else { + return getInteger0(index: applyOffset(index), limit: capacity, endianess: endianess) + } + + private func getInteger0(index: Int, limit: Int, endianess: Endianess = .Big) -> T? { + guard index + MemoryLayout.size <= limit else { return nil } let intBits = data.withUnsafeBytes({(bytePointer: UnsafePointer) -> T in @@ -239,6 +260,7 @@ public struct ByteBuffer { // TODO: Equatable, Comparable return toEndianess(value: intBits, endianess: endianess) } + public mutating func setInteger(index: Int, value: T, endianess: Endianess = .Big) -> Int? { let size = MemoryLayout.size if expandIfNeeded(index: index, size: size) { @@ -247,7 +269,7 @@ public struct ByteBuffer { // TODO: Equatable, Comparable withUnsafePointer(to: &v) { valPointer in valPointer.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size) { p in data.withUnsafeMutableBytes({ (dataPointer: UnsafeMutablePointer) -> Void in - dataPointer.advanced(by: index).assign(from: p, count: MemoryLayout.size) + dataPointer.advanced(by: applyOffset(index)).assign(from: p, count: MemoryLayout.size) }) } } @@ -264,7 +286,7 @@ public struct ByteBuffer { // TODO: Equatable, Comparable return nil } - // TODO: indexOf, bytesBefore, forEachByte, copy, reatinedSlice/slice, duplicate, direct buffer access?, backing byte array access? + // TODO: indexOf, bytesBefore, forEachByte, backing byte array access? // TODO: Generics to avoid this? public mutating func write(string: String) { @@ -313,6 +335,34 @@ public struct ByteBuffer { // TODO: Equatable, Comparable public mutating func skipBytes(num: Int) { moveReaderIndex(to: readerIndex + num) } + + /* + Returns a slice of this buffer's sub-region + */ + public func slice(from: Int, length: Int) -> ByteBuffer? { + guard from + length <= capacity else { + return nil + } + return ByteBuffer(allocator: allocator, data: data, offset: from, length: length, maxCapacity: maxCapacity) + } + + /** + Returns a slice of this buffer's readable bytes + */ + public func slice() -> ByteBuffer { + return slice(from: readerIndex, length: readableBytes)! + } + + /** + Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length). + */ + public mutating func readSlice(length: Int) -> ByteBuffer? { + if let buffer = slice(from: readerIndex, length: length) { + skipBytes(num: length) + return buffer + } + return nil + } private mutating func moveReaderIndex(to newIndex: Int) { precondition(newIndex >= 0 && newIndex <= writerIndex) diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index 48668df7f..1fde332d4 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -13,8 +13,8 @@ //===----------------------------------------------------------------------===// import Foundation -import NIO import XCTest +@testable import NIO class ByteBufferTest: XCTestCase { let allocator = ByteBufferAllocator() @@ -99,4 +99,47 @@ class ByteBufferTest: XCTestCase { XCTAssertEqual(v, buffer.readInteger()) XCTAssertEqual(0, buffer.readableBytes) } + + func testSlice() throws { + var buffer = try! allocator.buffer(capacity: 32) + XCTAssertEqual(MemoryLayout.size, buffer.writeInteger(value: UInt64.max)) + var slice = buffer.slice() + XCTAssertEqual(MemoryLayout.size, slice.readableBytes) + XCTAssertEqual(UInt64.max, slice.readInteger()) + XCTAssertEqual(MemoryLayout.size, buffer.readableBytes) + XCTAssertEqual(UInt64.max, buffer.readInteger()) + } + + func testSliceWithParams() throws { + var buffer = try! allocator.buffer(capacity: 32) + XCTAssertEqual(MemoryLayout.size, buffer.writeInteger(value: UInt64.max)) + var slice = buffer.slice(from: 0, length: MemoryLayout.size)! + XCTAssertEqual(MemoryLayout.size, slice.readableBytes) + XCTAssertEqual(UInt64.max, slice.readInteger()) + XCTAssertEqual(MemoryLayout.size, buffer.readableBytes) + XCTAssertEqual(UInt64.max, buffer.readInteger()) + } + + func testReadSlice() throws { + var buffer = try! allocator.buffer(capacity: 32) + XCTAssertEqual(MemoryLayout.size, buffer.writeInteger(value: UInt64.max)) + var slice = buffer.readSlice(length: buffer.readableBytes)! + XCTAssertEqual(MemoryLayout.size, slice.readableBytes) + XCTAssertEqual(UInt64.max, slice.readInteger()) + XCTAssertEqual(0, buffer.readableBytes) + let value: UInt64? = buffer.readInteger() + XCTAssertTrue(value == nil) + } + + func testSliceNoCopy() throws { + var buffer = try! allocator.buffer(capacity: 32) + XCTAssertEqual(MemoryLayout.size, buffer.writeInteger(value: UInt64.max)) + let slice = buffer.readSlice(length: buffer.readableBytes)! + + buffer.data.withUnsafeBytes { (ptr1: UnsafePointer) -> Void in + slice.data.withUnsafeBytes({ (ptr2: UnsafePointer) -> Void in + XCTAssertEqual(ptr1, ptr2) + }) + } + } }