Created
June 1, 2017 15:54
-
-
Save fel-cesar/3d659cc26b45a3d8de5cde5741844e5c to your computer and use it in GitHub Desktop.
Data extension for initialization with Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Data { | |
init(reading input: InputStream) { | |
self.init() | |
input.open() | |
let bufferSize = 1024 | |
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize) | |
while input.hasBytesAvailable { | |
let read = input.read(buffer, maxLength: bufferSize) | |
self.append(buffer, count: read) | |
// if (read == 0){ break} | |
} | |
buffer.deallocate(capacity: bufferSize) | |
input.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment