Skip to content

Commit 023a479

Browse files
committed
tweaks
1 parent c3f433d commit 023a479

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

packages/react-native-fast-io/ios/HybridInputStream.swift

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,20 @@ class HybridInputStream : HybridInputStreamSpec {
2323
let promise = Promise<ArrayBufferHolder>()
2424

2525
Task {
26-
let buffer = ArrayBufferHolder.allocate(size: Int(HybridStreamFactory.BUFFER_SIZE))
27-
let bytesRead = stream.read(buffer.data, maxLength: buffer.size)
26+
let size = Int(HybridStreamFactory.BUFFER_SIZE)
27+
let data = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
2828

29-
switch bytesRead {
30-
case 0: // End of stream
31-
promise.resolve(withResult: ArrayBufferHolder.allocate(size: 0))
32-
33-
case buffer.size: // Full buffer used
34-
promise.resolve(withResult: buffer)
35-
36-
case 1...: // Partially filled, needs slice
37-
let slice = ArrayBufferHolder.allocate(size: bytesRead)
38-
memcpy(slice.data, buffer.data, bytesRead)
39-
promise.resolve(withResult: slice)
40-
41-
default: // Error
42-
promise.reject(withError: stream.streamError ??
43-
RuntimeError.error(withMessage: "Unexpected error reading stream"))
29+
let bytesRead = stream.read(data, maxLength: size)
30+
31+
let deleteFunc = {
32+
data.deallocate()
33+
}
34+
35+
if (bytesRead >= 0) {
36+
promise.resolve(withResult: ArrayBufferHolder.wrap(dataWithoutCopy: data, size: bytesRead, onDelete: deleteFunc))
37+
} else {
38+
deleteFunc()
39+
promise.reject(withError: stream.streamError ?? RuntimeError.error(withMessage: "Unexpected error reading stream"))
4440
}
4541
}
4642

packages/react-native-fast-io/nitrogen/generated/ios/swift/HybridInputStreamSpecCxx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class HybridInputStreamSpecCxx {
105105
return { () -> bridge.PromiseHolder_std__shared_ptr_ArrayBuffer__ in
106106
let __promiseHolder = bridge.create_PromiseHolder_std__shared_ptr_ArrayBuffer__()
107107
__result
108-
.then({ __result in __promiseHolder.resolve(__result) })
108+
.then({ __result in __promiseHolder.resolve(__result.getArrayBuffer()) })
109109
.catch({ __error in __promiseHolder.reject(std.string(String(describing: __error))) })
110110
return __promiseHolder
111111
}()

0 commit comments

Comments
 (0)