From ed97628fa310c314c4a5cd8038445054b2991f07 Mon Sep 17 00:00:00 2001 From: Artem Redkin Date: Thu, 24 Oct 2019 03:23:34 +0300 Subject: [PATCH] fix NIO 2.9.0 deprecations (#61) Motivation: Usage of deprecated methods is bad. Modification: Fix usage of deprecated methods. Result: Fewer warnings. --- Package.swift | 2 +- Sources/NIOHTTPCompression/HTTPDecompression.swift | 9 ++++----- Sources/NIOHTTPCompression/HTTPResponseCompressor.swift | 2 +- .../HTTPRequestDecompressorTest.swift | 2 +- .../HTTPResponseCompressorTest.swift | 2 +- .../HTTPResponseDecompressorTest.swift | 2 +- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Package.swift b/Package.swift index 8aafa3b..208e35d 100644 --- a/Package.swift +++ b/Package.swift @@ -38,7 +38,7 @@ let package = Package( .library(name: "NIOHTTPCompression", targets: ["NIOHTTPCompression"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", from: "2.2.0"), + .package(url: "https://github.com/apple/swift-nio.git", from: "2.9.0"), ], targets: targets ) diff --git a/Sources/NIOHTTPCompression/HTTPDecompression.swift b/Sources/NIOHTTPCompression/HTTPDecompression.swift index 55f2e03..d606e84 100644 --- a/Sources/NIOHTTPCompression/HTTPDecompression.swift +++ b/Sources/NIOHTTPCompression/HTTPDecompression.swift @@ -86,8 +86,6 @@ public enum NIOHTTPDecompression { } mutating func decompress(part: inout ByteBuffer, buffer: inout ByteBuffer, originalLength: Int) throws { - buffer.reserveCapacity(part.readableBytes * 2) - self.inflated += try self.stream.inflatePart(input: &part, output: &buffer) if self.limit.exceeded(compressed: originalLength, decompressed: self.inflated) { @@ -114,6 +112,7 @@ public enum NIOHTTPDecompression { extension z_stream { mutating func inflatePart(input: inout ByteBuffer, output: inout ByteBuffer) throws -> Int { + let minimumCapacity = input.readableBytes * 2 var written = 0 try input.readWithUnsafeMutableReadableBytes { pointer in self.avail_in = UInt32(pointer.count) @@ -126,15 +125,15 @@ extension z_stream { self.next_out = nil } - written += try self.inflatePart(to: &output) + written += try self.inflatePart(to: &output, minimumCapacity: minimumCapacity) return pointer.count - Int(self.avail_in) } return written } - private mutating func inflatePart(to buffer: inout ByteBuffer) throws -> Int { - return try buffer.writeWithUnsafeMutableBytes { pointer in + private mutating func inflatePart(to buffer: inout ByteBuffer, minimumCapacity: Int) throws -> Int { + return try buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: minimumCapacity) { pointer in self.avail_out = UInt32(pointer.count) self.next_out = CNIOExtrasZlib_voidPtr_to_BytefPtr(pointer.baseAddress!) diff --git a/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift b/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift index 4752b9f..ad113fc 100644 --- a/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift +++ b/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift @@ -384,7 +384,7 @@ private extension z_stream { private mutating func deflateToBuffer(buffer: inout ByteBuffer, flag: Int32) -> Int32 { var rc = Z_OK - buffer.writeWithUnsafeMutableBytes { outputPtr in + buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: buffer.capacity) { outputPtr in let typedOutputPtr = UnsafeMutableBufferPointer(start: outputPtr.baseAddress!.assumingMemoryBound(to: UInt8.self), count: outputPtr.count) self.avail_out = UInt32(typedOutputPtr.count) diff --git a/Tests/NIOHTTPCompressionTests/HTTPRequestDecompressorTest.swift b/Tests/NIOHTTPCompressionTests/HTTPRequestDecompressorTest.swift index 4ff428f..b9b14e8 100644 --- a/Tests/NIOHTTPCompressionTests/HTTPRequestDecompressorTest.swift +++ b/Tests/NIOHTTPCompressionTests/HTTPRequestDecompressorTest.swift @@ -176,7 +176,7 @@ class HTTPRequestDecompressorTest: XCTestCase { stream.avail_in = UInt32(typedDataPtr.count) stream.next_in = typedDataPtr.baseAddress! - buffer.writeWithUnsafeMutableBytes { outputPtr in + buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: 0) { outputPtr in let typedOutputPtr = UnsafeMutableBufferPointer(start: outputPtr.baseAddress!.assumingMemoryBound(to: UInt8.self), count: outputPtr.count) stream.avail_out = UInt32(typedOutputPtr.count) diff --git a/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift b/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift index fa96a71..9548533 100644 --- a/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift +++ b/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift @@ -66,7 +66,7 @@ private extension ByteBuffer { @discardableResult mutating func writeWithUnsafeMutableUInt8Bytes(_ body: (UnsafeMutableBufferPointer) throws -> Int) rethrows -> Int { - return try self.writeWithUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> Int in + return try self.writeWithUnsafeMutableBytes(minimumWritableBytes: 0) { (ptr: UnsafeMutableRawBufferPointer) -> Int in let baseInputPointer = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) let inputBufferPointer = UnsafeMutableBufferPointer(start: baseInputPointer, count: ptr.count) return try body(inputBufferPointer) diff --git a/Tests/NIOHTTPCompressionTests/HTTPResponseDecompressorTest.swift b/Tests/NIOHTTPCompressionTests/HTTPResponseDecompressorTest.swift index 0bad7d3..d1249af 100644 --- a/Tests/NIOHTTPCompressionTests/HTTPResponseDecompressorTest.swift +++ b/Tests/NIOHTTPCompressionTests/HTTPResponseDecompressorTest.swift @@ -156,7 +156,7 @@ class HTTPResponseDecompressorTest: XCTestCase { stream.avail_in = UInt32(typedDataPtr.count) stream.next_in = typedDataPtr.baseAddress! - buffer.writeWithUnsafeMutableBytes { outputPtr in + buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: 0) { outputPtr in let typedOutputPtr = UnsafeMutableBufferPointer(start: outputPtr.baseAddress!.assumingMemoryBound(to: UInt8.self), count: outputPtr.count) stream.avail_out = UInt32(typedOutputPtr.count)