Skip to content

feat: run streaming read/write operations in a Kotlin coroutine #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@ import com.margelo.nitro.core.ArrayBuffer
import com.margelo.nitro.core.Promise
import java.io.InputStream

class HybridInputStream(private val stream: InputStream) : HybridInputStreamSpec() {
class HybridInputStream(public val stream: InputStream) : HybridInputStreamSpec() {
override fun read(): Promise<ArrayBuffer> {
return Promise<ArrayBuffer>().apply {
try {
val bytes = ByteArray(HybridStreamFactory.BUFFER_SIZE)
val bytesRead = stream.read(bytes, 0, bytes.size)

when {
bytesRead == -1 -> {
// End of stream
resolve(ArrayBuffer.allocate(0))
}
bytesRead > 0 -> {
val arrayBuffer = ArrayBuffer.allocate(bytesRead)

val destBuffer = arrayBuffer.getBuffer(false)
destBuffer.put(bytes, 0, bytesRead)

resolve(arrayBuffer)
}
else -> {
// Error case
reject(Error("Unexpected error reading stream"))
}
return Promise.async {
val bytes = ByteArray(HybridStreamFactory.BUFFER_SIZE)
val bytesRead = stream.read(bytes, 0, bytes.size)

when {
bytesRead == -1 -> {
// End of stream
ArrayBuffer.allocate(0)
}
bytesRead > 0 -> {
val arrayBuffer = ArrayBuffer.allocate(bytesRead)

val destBuffer = arrayBuffer.getBuffer(false)
destBuffer.put(bytes, 0, bytesRead)

arrayBuffer
}
else -> {
// Error case
throw Error("Unexpected error reading stream")
}
} catch (e: Exception) {
reject(Error(e.message))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ class HybridOutputStream(private val stream: OutputStream) : HybridOutputStreamS
val bytes = ByteArray(buffer.size)
byteBuffer.get(bytes)

return Promise<Unit>().apply {
try {
stream.write(bytes)
resolve(Unit)
} catch (e: Exception) {
reject(Error(e.message))
}
return Promise.async {
stream.write(bytes)
}
}

Expand Down
10 changes: 2 additions & 8 deletions packages/react-native-fast-io/src/w3c/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ export class CompressionStream implements globalThis.CompressionStream {

const { readable, writable } = new TransformStream<Uint8Array>({
transform(chunk, controller) {
try {
const compressedData = compressor.compress(chunk.buffer)
controller.enqueue(new Uint8Array(compressedData))
} catch (error) {
console.error(error)
}
const compressedData = compressor.compress(chunk.buffer)
controller.enqueue(new Uint8Array(compressedData))
},
flush(controller) {
console.log('flushing')
const finalData = compressor.finalize()
if (finalData.byteLength > 0) {
console.log(finalData.byteLength)
controller.enqueue(new Uint8Array(finalData))
}
},
Expand Down