Reading an InputStream into a Data object
Asked Answered
G

2

10

In Swift 3.x, we usually handle binary data using Data; from it you can generate most other important types, and there are useful functions on it.

But how do I create a Data from an InputStream? Is there a nice way?

Gallon answered 2/3, 2017 at 16:40 Comment(0)
G
23

I could not find a nice way. We can create a nice-ish wrapper around the unsafe stuff:

extension Data {
    init(reading input: InputStream) throws {
        self.init()
        input.open()
        defer {
            input.close()
        }

        let bufferSize = 1024
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
        defer {
            buffer.deallocate()
        }
        while input.hasBytesAvailable {
            let read = input.read(buffer, maxLength: bufferSize)
            if read < 0 {
                //Stream error occured
                throw input.streamError!
            } else if read == 0 {
                //EOF
                break
            }
            self.append(buffer, count: read)
        }
    }
}

This is for Swift 5. Find full code with test (and a variant that reads only some of the stream) here.

Gallon answered 2/3, 2017 at 16:40 Comment(1)
good use of the defer statement.Gretagretal
M
1

above the code, It can be infinite loop. When I convert httpbodyInpustream to data, it happend. So I add a condition.

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)
            if (read == 0) {
                break  // added
            }
            self.append(buffer, count: read)
        }
        buffer.deallocate(capacity: bufferSize)

        input.close()
    }
}
Miramirabeau answered 18/5, 2017 at 3:54 Comment(3)
Sounds like that stream is broken; hasBytesAvailable should return false, shouldn't it? Not sure what the semantics of hasBytesAvailable == true but read == 0 is; sounds like an error scenario to me.Gallon
@Gallon InputStream.read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int returns -1 if there was an error, 0 if there's no more to read, and >1 if there are bytes left to read.Loyalty
hasBytesAvailable is allowed to return true if a read has to be performed to actually check whether bytes are available (read the NSInputStream documentation). A proper implementation has to check the read result code. I modified the accepted answer with correct error handling.Vermicide

© 2022 - 2024 — McMap. All rights reserved.