Skip to content

Commit 791295d

Browse files
committed
tmp
1 parent c8ba7d5 commit 791295d

File tree

4 files changed

+61
-48
lines changed

4 files changed

+61
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
package com.margelo.nitro.fastio
22

3+
import java.io.PipedInputStream
4+
import java.io.PipedOutputStream
5+
36
class HybridDuplexStream : HybridDuplexStreamSpec() {
4-
override var inputStream: HybridInputStreamSpec
5-
get() = throw NotImplementedError("HybridDuplexStream.inputStream getter not implemented")
6-
set(_) = throw NotImplementedError("HybridDuplexStream.inputStream setter not implemented")
7+
private val pipeIn = PipedInputStream(HybridStreamFactory.BUFFER_SIZE)
8+
private val pipeOut = PipedOutputStream(pipeIn)
9+
10+
override var inputStream: HybridInputStreamSpec = HybridInputStream(pipeIn).also {
11+
System.err.println("Created input stream wrapper")
12+
}
13+
14+
override var outputStream: HybridOutputStreamSpec = HybridOutputStream(pipeOut).also {
15+
System.err.println("Created output stream wrapper")
16+
}
717

8-
override var outputStream: HybridOutputStreamSpec
9-
get() = throw NotImplementedError("HybridDuplexStream.outputStream getter not implemented")
10-
set(_) = throw NotImplementedError("HybridDuplexStream.outputStream setter not implemented")
18+
override val memorySize: Long = 0L
1119

12-
override val memorySize: Long
13-
get() = 0L
20+
fun close() {
21+
try {
22+
System.err.println("Closing duplex stream")
23+
pipeOut.close()
24+
pipeIn.close()
25+
} catch (e: Exception) {
26+
System.err.println("Error closing duplex stream: ${e.message}")
27+
}
28+
}
1429
}

packages/react-native-fast-io/android/src/main/java/com/margelo/nitro/fastio/HybridInputStream.kt

+21-15
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@ package com.margelo.nitro.fastio
22

33
import com.margelo.nitro.core.ArrayBuffer
44
import com.margelo.nitro.core.Promise
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.withContext
57
import java.io.InputStream
68

79
class HybridInputStream(val stream: InputStream) : HybridInputStreamSpec() {
810
override fun read(): Promise<ArrayBuffer> {
911
return Promise.async {
10-
val bytes = ByteArray(HybridStreamFactory.BUFFER_SIZE)
11-
val bytesRead = stream.read(bytes, 0, bytes.size)
12+
withContext(Dispatchers.IO) {
13+
val bytes = ByteArray(HybridStreamFactory.BUFFER_SIZE)
14+
val bytesRead = stream.read(bytes, 0, bytes.size)
1215

13-
when {
14-
bytesRead == -1 -> {
15-
val emptyBuffer = ArrayBuffer.allocate(0)
16-
emptyBuffer
17-
}
18-
bytesRead > 0 -> {
19-
val arrayBuffer = ArrayBuffer.allocate(bytesRead)
16+
when {
17+
bytesRead == -1 -> {
18+
val emptyBuffer = ArrayBuffer.allocate(0)
19+
emptyBuffer
20+
}
2021

21-
val destBuffer = arrayBuffer.getBuffer(false)
22-
destBuffer.put(bytes, 0, bytesRead)
22+
bytesRead > 0 -> {
23+
val arrayBuffer = ArrayBuffer.allocate(bytesRead)
2324

24-
arrayBuffer
25-
}
26-
else -> {
27-
throw Error("Unexpected error reading stream")
25+
val destBuffer = arrayBuffer.getBuffer(false)
26+
destBuffer.put(bytes, 0, bytesRead)
27+
28+
arrayBuffer
29+
}
30+
31+
else -> {
32+
throw Error("Unexpected error reading stream")
33+
}
2834
}
2935
}
3036
}

packages/react-native-fast-io/android/src/main/java/com/margelo/nitro/fastio/HybridNetwork.kt

+12-24
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,33 @@ package com.margelo.nitro.fastio
33
import com.margelo.nitro.core.Promise
44
import java.net.HttpURLConnection
55
import java.net.URL
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.withContext
68

79
class HybridNetwork : HybridNetworkSpec() {
810
override fun request(opts: RequestOptions): Promise<Unit> {
911
return Promise.async {
10-
val connection = URL(opts.url).openConnection() as HttpURLConnection
12+
withContext(Dispatchers.IO) {
13+
val connection = URL(opts.url).openConnection() as HttpURLConnection
1114

12-
try {
1315
connection.apply {
1416
requestMethod = opts.method.name.uppercase()
1517
doInput = true
1618
doOutput = opts.body != null
1719

18-
connect()
19-
20-
opts.body?.let { hybridStream ->
21-
(hybridStream as HybridInputStream).stream.use { input ->
22-
outputStream.buffered().use { output ->
23-
val buffer = ByteArray(HybridStreamFactory.BUFFER_SIZE)
24-
var bytesRead: Int
25-
var totalBytes = 0
26-
27-
while (input.read(buffer).also { bytesRead = it } != -1) {
28-
output.write(buffer, 0, bytesRead)
29-
output.flush()
30-
totalBytes += bytesRead
31-
}
32-
}
33-
}
34-
}
20+
// opts.body?.let { hybridStream ->
21+
// (hybridStream as HybridInputStream).stream.use { input ->
22+
// outputStream.buffered().use { output ->
23+
// input.copyTo(output, HybridStreamFactory.BUFFER_SIZE)
24+
// }
25+
// }
26+
// }
3527

3628
val code = responseCode
37-
3829
if (code !in 200..299) {
39-
val errorBody = errorStream?.bufferedReader()?.readText() ?: "Unknown error"
40-
throw Error("HTTP Error $code: $errorBody")
30+
throw Error("HTTP Error: $code")
4131
}
4232
}
43-
} finally {
44-
connection.disconnect()
4533
}
4634
}
4735
}

packages/react-native-fast-io/android/src/main/java/com/margelo/nitro/fastio/HybridOutputStream.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.margelo.nitro.fastio
22

33
import com.margelo.nitro.core.ArrayBuffer
44
import com.margelo.nitro.core.Promise
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.withContext
57
import java.io.OutputStream
68

79
class HybridOutputStream(private val stream: OutputStream) : HybridOutputStreamSpec() {
@@ -11,7 +13,9 @@ class HybridOutputStream(private val stream: OutputStream) : HybridOutputStreamS
1113
byteBuffer.get(bytes)
1214

1315
return Promise.async {
14-
stream.write(bytes)
16+
withContext(Dispatchers.IO) {
17+
stream.write(bytes)
18+
}
1519
}
1620
}
1721

0 commit comments

Comments
 (0)