Skip to content

Instantly share code, notes, and snippets.

@fel-cesar
Created June 1, 2017 15:54
Show Gist options
  • Save fel-cesar/3d659cc26b45a3d8de5cde5741844e5c to your computer and use it in GitHub Desktop.
Save fel-cesar/3d659cc26b45a3d8de5cde5741844e5c to your computer and use it in GitHub Desktop.
Data extension for initialization with Data
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