Skip to content

Instantly share code, notes, and snippets.

@reitzig
Last active April 15, 2022 03:47
Show Gist options
  • Select an option

  • Save reitzig/29d38b948812454a8dedc00a9f61f44f to your computer and use it in GitHub Desktop.

Select an option

Save reitzig/29d38b948812454a8dedc00a9f61f44f to your computer and use it in GitHub Desktop.

Revisions

  1. reitzig revised this gist Apr 24, 2019. 2 changed files with 2 additions and 3 deletions.
    4 changes: 1 addition & 3 deletions Data.swift
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ extension Data {

    - Parameter reading: The input stream to read data from.
    - Parameter byteCount: The maximum number of bytes to read from `reading`.
    - Note: Closes the specified stream.
    - Note: Does _not_ close the specified stream.
    */
    init(reading input: InputStream, for byteCount: Int) {
    self.init()
    @@ -36,7 +36,5 @@ extension Data {
    let read = input.read(buffer, maxLength: byteCount)
    self.append(buffer, count: read)
    buffer.deallocate()

    input.close()
    }
    }
    1 change: 1 addition & 0 deletions DataTest.swift
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ class DataTests: XCTestCase {
    ].forEach { (bytes) in
    let referenceData = Data(bytes)
    let stream = InputStream(data: referenceData)
    defer { stream.close() }
    XCTAssertEqual(
    Data(reading: stream, for: 3),
    referenceData[0..<(min(3, referenceData.count))]
  2. reitzig revised this gist Apr 24, 2019. 2 changed files with 37 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions Data.swift
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,24 @@ extension Data {

    input.close()
    }

    /**
    Consumes the specified input stream for up to `byteCount` bytes,
    creating a new Data object with its content.

    - Parameter reading: The input stream to read data from.
    - Parameter byteCount: The maximum number of bytes to read from `reading`.
    - Note: Closes the specified stream.
    */
    init(reading input: InputStream, for byteCount: Int) {
    self.init()
    input.open()

    let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: byteCount)
    let read = input.read(buffer, maxLength: byteCount)
    self.append(buffer, count: read)
    buffer.deallocate()

    input.close()
    }
    }
    17 changes: 17 additions & 0 deletions DataTest.swift
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,21 @@ class DataTests: XCTestCase {
    XCTAssertEqual(Data(reading: stream), referenceData)
    }
    }

    func testInitFromStreamWithMaxLength() {
    // Tests some random values, plus the empty case
    [[UInt8](),
    [0b00001001],
    [0b01000000, 0b01001011],
    [0b01011110, 0b11101111, 0b00011001],
    [0b01101100, 0b00101010, 0b10111001, 0b10000111]
    ].forEach { (bytes) in
    let referenceData = Data(bytes)
    let stream = InputStream(data: referenceData)
    XCTAssertEqual(
    Data(reading: stream, for: 3),
    referenceData[0..<(min(3, referenceData.count))]
    )
    }
    }
    }
  3. reitzig revised this gist Apr 24, 2019. 2 changed files with 19 additions and 20 deletions.
    17 changes: 8 additions & 9 deletions Data.swift
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,22 @@
    extension Data {
    /**
    Consumes the specified input stream, creating a new Data object
    with its content.

    - Parameter reading: The input stream to read data from.
    - Note: Closes the specified stream.
    */
    Consumes the specified input stream, creating a new Data object
    with its content.
    - Parameter reading: The input stream to read data from.
    - Note: Closes the specified stream.
    */
    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)
    }
    buffer.deallocate(capacity: bufferSize)

    buffer.deallocate()
    input.close()
    }
    }
    22 changes: 11 additions & 11 deletions DataTest.swift
    Original file line number Diff line number Diff line change
    @@ -4,17 +4,17 @@ import XCTest
    class DataTests: XCTestCase {
    func testInitFromStream() {
    // Tests some random values, plus the empty case
    [(bytes: [UInt8]()),
    (bytes: [0b00001001]),
    (bytes: [0b01000000, 0b01001011]),
    (bytes: [0b01011110, 0b11101111, 0b00011001]),
    (bytes: [0b01101100, 0b00101010, 0b10111001, 0b10000111]),
    (bytes: [0b00011011, 0b00010111, 0b00011000, 0b01100100, 0b10110000]),
    (bytes: [UInt8](repeating: 77, count: 2049)) // something large; 2*buffersize + 1
    ].forEach { (bytes) in
    let referenceData = Data(bytes: bytes)
    let stream = InputStream(data: referenceData)
    XCTAssertEqual(Data(reading: stream), referenceData)
    [[UInt8](),
    [0b00001001],
    [0b01000000, 0b01001011],
    [0b01011110, 0b11101111, 0b00011001],
    [0b01101100, 0b00101010, 0b10111001, 0b10000111],
    [0b00011011, 0b00010111, 0b00011000, 0b01100100, 0b10110000],
    [UInt8](repeating: 77, count: 2049) // something large; 2*buffersize + 1
    ].forEach { (bytes) in
    let referenceData = Data(bytes)
    let stream = InputStream(data: referenceData)
    XCTAssertEqual(Data(reading: stream), referenceData)
    }
    }
    }
  4. reitzig created this gist Mar 2, 2017.
    23 changes: 23 additions & 0 deletions Data.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    extension Data {
    /**
    Consumes the specified input stream, creating a new Data object
    with its content.

    - Parameter reading: The input stream to read data from.
    - Note: Closes the specified stream.
    */
    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)
    }
    buffer.deallocate(capacity: bufferSize)

    input.close()
    }
    }
    20 changes: 20 additions & 0 deletions DataTest.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import Foundation
    import XCTest

    class DataTests: XCTestCase {
    func testInitFromStream() {
    // Tests some random values, plus the empty case
    [(bytes: [UInt8]()),
    (bytes: [0b00001001]),
    (bytes: [0b01000000, 0b01001011]),
    (bytes: [0b01011110, 0b11101111, 0b00011001]),
    (bytes: [0b01101100, 0b00101010, 0b10111001, 0b10000111]),
    (bytes: [0b00011011, 0b00010111, 0b00011000, 0b01100100, 0b10110000]),
    (bytes: [UInt8](repeating: 77, count: 2049)) // something large; 2*buffersize + 1
    ].forEach { (bytes) in
    let referenceData = Data(bytes: bytes)
    let stream = InputStream(data: referenceData)
    XCTAssertEqual(Data(reading: stream), referenceData)
    }
    }
    }