From 17bb442c7841dd0b703c7b1c741f46f2d84bfab0 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 17 Dec 2020 08:37:26 +0200 Subject: [PATCH] ensure socket inputstream is buffered --- .../sparrowwallet/sparrow/net/TcpTransport.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java index e1401297..33b2ed96 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java @@ -130,9 +130,11 @@ public class TcpTransport implements Transport, Closeable { Thread.currentThread().interrupt(); } + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + while(running) { try { - String received = readInputStream(); + String received = readInputStream(in); if(received.contains("method") && !received.contains("error")) { //Handle subscription notification jsonRpcServer.handle(received, subscriptionService); @@ -157,13 +159,21 @@ public class TcpTransport implements Transport, Closeable { } } } + } catch(IOException e) { + log.error("Error opening socket inputstream", e); + if(running) { + lastException = e; + reading = false; + readingCondition.signal(); + //Allow this thread to terminate as we will need to reconnect with a new transport anyway + running = false; + } } finally { readLock.unlock(); } } - protected String readInputStream() throws IOException { - BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + protected String readInputStream(BufferedReader in) throws IOException { String response = in.readLine(); if(response == null) {