@@ -17,6 +17,7 @@ limitations under the License.
17
17
package info.p2sh.clightning.rpc
18
18
19
19
import com.google.gson.Gson
20
+ import com.google.gson.JsonParser
20
21
import info.p2sh.clightning.http.JsonRpcError
21
22
import info.p2sh.clightning.http.JsonRpcRequest
22
23
import info.p2sh.clightning.http.JsonRpcResponse
@@ -29,6 +30,8 @@ import java.nio.CharBuffer
29
30
import java.nio.channels.Channels
30
31
31
32
class RpcClient (private val path : String , private val gson : Gson ) {
33
+ private val jsonParser = JsonParser ()
34
+
32
35
fun sendRequest (request : JsonRpcRequest ): JsonRpcResponse {
33
36
val socketFile = File (path)
34
37
val address = UnixSocketAddress (socketFile)
@@ -39,16 +42,23 @@ class RpcClient(private val path: String, private val gson: Gson) {
39
42
it.flush()
40
43
41
44
InputStreamReader (Channels .newInputStream(channel)).use {
42
- val builder = StringBuilder ()
45
+ var result = " "
46
+ var isValidJson = false
43
47
44
48
do {
45
- val result = CharBuffer .allocate(8192 )
46
- it.read(result)
47
- result.flip()
48
- builder.append(result)
49
- } while (result.length == 8192 )
49
+ val chunk = CharBuffer .allocate(8192 )
50
+ it.read(chunk)
51
+ chunk.flip()
52
+ result + = chunk.toString()
53
+
54
+ try {
55
+ jsonParser.parse(result)
56
+ isValidJson = true
57
+ } catch (e: Exception ) {
58
+ }
59
+ } while (chunk.isNotEmpty() && ! isValidJson)
50
60
51
- builder.toString()
61
+ result
52
62
}
53
63
}
54
64
0 commit comments