Skip to content

Commit 4bbf10a

Browse files
authored
Merge pull request #148 from gjeanmart/issue/142
Fix issue #142 - NullPointerException when incorrect SSL flag is set to IPFS constructor
2 parents 694cef4 + e252d59 commit 4bbf10a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,9 @@ private static byte[] get(URL target, int timeout) throws IOException {
692692
} catch (SocketTimeoutException e) {
693693
throw new RuntimeException(String.format("timeout (%d ms) has been exceeded", timeout));
694694
} catch (IOException e) {
695-
String err = new String(readFully(conn.getErrorStream()));
695+
String err = Optional.ofNullable(conn.getErrorStream())
696+
.map(s->new String(readFully(s)))
697+
.orElse(e.getMessage());
696698
throw new RuntimeException("IOException contacting IPFS daemon.\nTrailer: " + conn.getHeaderFields().get("Trailer") + " " + err, e);
697699
}
698700
}
@@ -765,13 +767,18 @@ private static byte[] post(URL target, byte[] body, Map<String, String> headers,
765767
return readFully(in);
766768
}
767769

768-
private static final byte[] readFully(InputStream in) throws IOException {
769-
ByteArrayOutputStream resp = new ByteArrayOutputStream();
770-
byte[] buf = new byte[4096];
771-
int r;
772-
while ((r=in.read(buf)) >= 0)
773-
resp.write(buf, 0, r);
774-
return resp.toByteArray();
770+
private static final byte[] readFully(InputStream in) {
771+
try {
772+
ByteArrayOutputStream resp = new ByteArrayOutputStream();
773+
byte[] buf = new byte[4096];
774+
int r;
775+
while ((r=in.read(buf)) >= 0)
776+
resp.write(buf, 0, r);
777+
return resp.toByteArray();
778+
779+
} catch(IOException ex) {
780+
throw new RuntimeException("Error reading InputStrean", ex);
781+
}
775782
}
776783

777784
private static boolean detectSSL(MultiAddress multiaddress) {

0 commit comments

Comments
 (0)