Skip to content

Commit e252d59

Browse files
author
gjeanmart
committed
Fix issue#142 - NullPointerException when incorrect SSL flag is set to
IPFS constructor
1 parent 5a8bcbc commit e252d59

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
@@ -679,7 +679,9 @@ private static byte[] get(URL target) throws IOException {
679679
} catch (ConnectException e) {
680680
throw new RuntimeException("Couldn't connect to IPFS daemon at "+target+"\n Is IPFS running?");
681681
} catch (IOException e) {
682-
String err = new String(readFully(conn.getErrorStream()));
682+
String err = Optional.ofNullable(conn.getErrorStream())
683+
.map(s->new String(readFully(s)))
684+
.orElse(e.getMessage());
683685
throw new RuntimeException("IOException contacting IPFS daemon.\nTrailer: " + conn.getHeaderFields().get("Trailer") + " " + err, e);
684686
}
685687
}
@@ -756,13 +758,18 @@ private static byte[] post(URL target, byte[] body, Map<String, String> headers)
756758
return readFully(in);
757759
}
758760

759-
private static final byte[] readFully(InputStream in) throws IOException {
760-
ByteArrayOutputStream resp = new ByteArrayOutputStream();
761-
byte[] buf = new byte[4096];
762-
int r;
763-
while ((r=in.read(buf)) >= 0)
764-
resp.write(buf, 0, r);
765-
return resp.toByteArray();
761+
private static final byte[] readFully(InputStream in) {
762+
try {
763+
ByteArrayOutputStream resp = new ByteArrayOutputStream();
764+
byte[] buf = new byte[4096];
765+
int r;
766+
while ((r=in.read(buf)) >= 0)
767+
resp.write(buf, 0, r);
768+
return resp.toByteArray();
769+
770+
} catch(IOException ex) {
771+
throw new RuntimeException("Error reading InputStrean", ex);
772+
}
766773
}
767774

768775
private static boolean detectSSL(MultiAddress multiaddress) {

0 commit comments

Comments
 (0)