Skip to content

Commit b6bdb36

Browse files
committed
Fix dht.findProvs to return all results
1 parent a1e3f5d commit b6bdb36

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,10 @@ public String resolve(Multihash hash) throws IOException {
407407
}
408408

409409
public class DHT {
410-
public Map findprovs(Multihash hash) throws IOException {
411-
return retrieveMap("dht/findprovs?arg=" + hash);
410+
public List<Map<String, Object>> findprovs(Multihash hash) throws IOException {
411+
return getAndParseStream("dht/findprovs?arg=" + hash).stream()
412+
.map(x -> (Map<String, Object>) x)
413+
.collect(Collectors.toList());
412414
}
413415

414416
public Map query(Multihash peerId) throws IOException {
@@ -697,6 +699,25 @@ private void getObjectStream(InputStream in, Consumer<byte[]> processor, Consume
697699
}
698700
}
699701

702+
private List<Object> getAndParseStream(String path) throws IOException {
703+
InputStream in = retrieveStream(path);
704+
byte LINE_FEED = (byte)10;
705+
706+
ByteArrayOutputStream resp = new ByteArrayOutputStream();
707+
708+
byte[] buf = new byte[4096];
709+
int r;
710+
List<Object> res = new ArrayList<>();
711+
while ((r = in.read(buf)) >= 0) {
712+
resp.write(buf, 0, r);
713+
if (buf[r - 1] == LINE_FEED) {
714+
res.add(JSONParser.parse(new String(resp.toByteArray())));
715+
resp.reset();
716+
}
717+
}
718+
return res;
719+
}
720+
700721
private InputStream retrieveStream(String path) throws IOException {
701722
URL target = new URL("http", host, port, version + path);
702723
return IPFS.getStream(target);

0 commit comments

Comments
 (0)