Skip to content

Commit 63db3c5

Browse files
committed
temporary fix on Android
1 parent 791295d commit 63db3c5

File tree

3 files changed

+37
-50
lines changed

3 files changed

+37
-50
lines changed

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

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

33
import com.margelo.nitro.core.ArrayBuffer
44
import com.margelo.nitro.core.Promise
5+
import kotlinx.coroutines.CoroutineScope
56
import kotlinx.coroutines.Dispatchers
6-
import kotlinx.coroutines.withContext
77
import java.io.InputStream
88

99
class HybridInputStream(val stream: InputStream) : HybridInputStreamSpec() {
1010
override fun read(): Promise<ArrayBuffer> {
11-
return Promise.async {
12-
withContext(Dispatchers.IO) {
13-
val bytes = ByteArray(HybridStreamFactory.BUFFER_SIZE)
14-
val bytesRead = stream.read(bytes, 0, bytes.size)
15-
16-
when {
17-
bytesRead == -1 -> {
18-
val emptyBuffer = ArrayBuffer.allocate(0)
19-
emptyBuffer
20-
}
21-
22-
bytesRead > 0 -> {
23-
val arrayBuffer = ArrayBuffer.allocate(bytesRead)
24-
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-
}
11+
return Promise.async(CoroutineScope(Dispatchers.IO)) {
12+
val bytes = ByteArray(HybridStreamFactory.BUFFER_SIZE)
13+
val bytesRead = stream.read(bytes, 0, bytes.size)
14+
15+
when {
16+
bytesRead == -1 -> {
17+
val emptyBuffer = ArrayBuffer.allocate(0)
18+
emptyBuffer
19+
}
20+
bytesRead > 0 -> {
21+
val arrayBuffer = ArrayBuffer.allocate(bytesRead)
22+
val destBuffer = arrayBuffer.getBuffer(false)
23+
destBuffer.put(bytes, 0, bytesRead)
24+
arrayBuffer
25+
}
26+
else -> {
27+
throw Error("Unexpected error reading stream")
3428
}
3529
}
3630
}

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

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
package com.margelo.nitro.fastio
22

33
import com.margelo.nitro.core.Promise
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
46
import java.net.HttpURLConnection
57
import java.net.URL
6-
import kotlinx.coroutines.Dispatchers
7-
import kotlinx.coroutines.withContext
88

99
class HybridNetwork : HybridNetworkSpec() {
1010
override fun request(opts: RequestOptions): Promise<Unit> {
11-
return Promise.async {
12-
withContext(Dispatchers.IO) {
13-
val connection = URL(opts.url).openConnection() as HttpURLConnection
14-
15-
connection.apply {
16-
requestMethod = opts.method.name.uppercase()
17-
doInput = true
18-
doOutput = opts.body != null
11+
Promise.async(CoroutineScope(Dispatchers.IO)) {
12+
val connection = URL(opts.url).openConnection() as HttpURLConnection
13+
connection.apply {
14+
requestMethod = opts.method.name.uppercase()
15+
doInput = true
16+
doOutput = opts.body != null
1917

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-
// }
27-
28-
val code = responseCode
29-
if (code !in 200..299) {
30-
throw Error("HTTP Error: $code")
18+
opts.body?.let { hybridStream ->
19+
(hybridStream as HybridInputStream).stream.use { input ->
20+
outputStream.buffered().use { output ->
21+
input.copyTo(output, HybridStreamFactory.BUFFER_SIZE)
22+
}
3123
}
3224
}
25+
26+
if (responseCode !in 200..299) {
27+
throw Error("HTTP Error: $responseCode")
28+
}
3329
}
3430
}
31+
return Promise.resolved(Unit)
3532
}
3633

3734
override val memorySize: Long

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ 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
75
import java.io.OutputStream
86

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

1513
return Promise.async {
16-
withContext(Dispatchers.IO) {
17-
stream.write(bytes)
18-
}
14+
stream.write(bytes)
1915
}
2016
}
2117

0 commit comments

Comments
 (0)