@@ -604,7 +604,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
604
604
605
605
// MARK: - Base64 Methods
606
606
607
- private func estimateBase64Size( length: Int ) -> Int {
607
+ internal static func estimateBase64Size( length: Int ) -> Int {
608
608
// Worst case allow for 64bytes + \r\n per line 48 input bytes => 66 output bytes
609
609
return ( ( length + 47 ) * 66 ) / 48
610
610
}
@@ -614,7 +614,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
614
614
let dataLength = self . length
615
615
if dataLength == 0 { return " " }
616
616
617
- let capacity = estimateBase64Size ( length: dataLength)
617
+ let capacity = NSData . estimateBase64Size ( length: dataLength)
618
618
let ptr = UnsafeMutableRawPointer . allocate ( byteCount: capacity, alignment: 4 )
619
619
defer { ptr. deallocate ( ) }
620
620
let buffer = UnsafeMutableRawBufferPointer ( start: ptr, count: capacity)
@@ -628,11 +628,13 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
628
628
let dataLength = self . length
629
629
if dataLength == 0 { return Data ( ) }
630
630
631
- let capacity = estimateBase64Size ( length: dataLength)
631
+ let inputBuffer = UnsafeRawBufferPointer ( start: self . bytes, count: self . length)
632
+
633
+ let capacity = NSData . estimateBase64Size ( length: dataLength)
632
634
let ptr = UnsafeMutableRawPointer . allocate ( byteCount: capacity, alignment: 4 )
633
- let buffer = UnsafeMutableRawBufferPointer ( start: ptr, count: capacity)
634
- let length = NSData . base64EncodeBytes ( self , options: options, buffer: buffer)
635
+ let outputBuffer = UnsafeMutableRawBufferPointer ( start: ptr, count: capacity)
635
636
637
+ let length = NSData . base64EncodeBytes ( inputBuffer, options: options, buffer: outputBuffer)
636
638
return Data ( bytesNoCopy: ptr, count: length, deallocator: . custom( { ( ptr, length) in
637
639
ptr. deallocate ( )
638
640
} ) )
@@ -761,13 +763,14 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
761
763
/**
762
764
This method encodes data in Base64.
763
765
764
- - parameter data: The NSData object you want to encode
766
+ - parameter dataBuffer: The UnsafeRawBufferPointer buffer to encode
765
767
- parameter options: Options for formatting the result
766
768
- parameter buffer: The buffer to write the bytes into
767
769
- returns: The number of bytes written into the buffer
768
- */
769
- private static func base64EncodeBytes( _ data: NSData , options: Base64EncodingOptions = [ ] , buffer: UnsafeMutableRawBufferPointer ) -> Int {
770
770
771
+ NOTE: dataBuffer would be better expressed as a <T: Collection> where T.Element == UInt8, T.Index == Int but this currently gives much poorer performance.
772
+ */
773
+ static func base64EncodeBytes( _ dataBuffer: UnsafeRawBufferPointer , options: Base64EncodingOptions = [ ] , buffer: UnsafeMutableRawBufferPointer ) -> Int {
771
774
// Use a StaticString for lookup of values 0-63 -> ASCII values
772
775
let base64Chars = StaticString ( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ " )
773
776
assert ( base64Chars. utf8CodeUnitCount == 64 )
@@ -811,7 +814,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
811
814
}
812
815
813
816
// Read three bytes at a time, which convert to 4 ASCII characters, allowing for byte2 and byte3 being nil
814
- let dataBuffer = UnsafeRawBufferPointer ( start : data . bytes , count : data . length )
817
+
815
818
var inputIndex = 0
816
819
var outputIndex = 0
817
820
var bytesLeft = dataBuffer. count
0 commit comments