Skip to content

Commit f7e8cf6

Browse files
committed
Added catStream for getting contents of arbitrarily large files.
1 parent f6102e1 commit f7e8cf6

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public byte[] get(Multihash hash) throws IOException {
7171
return retrieve("get/" + hash);
7272
}
7373

74+
public InputStream catStream(Multihash hash) throws IOException {
75+
return retrieveStream("cat/" + hash);
76+
}
77+
7478
public Map refs(Multihash hash, boolean recursive) throws IOException {
7579
return retrieveMap("refs?arg=" + hash +"&r="+recursive);
7680
}
@@ -433,6 +437,19 @@ private static byte[] get(URL target) throws IOException {
433437
}
434438
}
435439

440+
private InputStream retrieveStream(String path) throws IOException {
441+
URL target = new URL("http", host, port, version + path);
442+
return IPFS.getStream(target);
443+
}
444+
445+
private static InputStream getStream(URL target) throws IOException {
446+
HttpURLConnection conn = (HttpURLConnection) target.openConnection();
447+
conn.setRequestMethod("GET");
448+
conn.setRequestProperty("Content-Type", "application/json");
449+
450+
return conn.getInputStream();
451+
}
452+
436453
private Map postMap(String path, byte[] body, Map<String, String> headers) throws IOException {
437454
URL target = new URL("http", host, port, version + path);
438455
return (Map) JSONParser.parse(new String(post(target, body, headers)));

src/test/java/org/ipfs/api/Test.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.ipfs.api;
22

3-
import java.io.File;
4-
import java.io.IOException;
3+
import java.io.*;
54
import java.util.Arrays;
65
import java.util.List;
76
import java.util.Map;
@@ -35,6 +34,34 @@ public void largeFileTest() {
3534
fileTest(largeFile);
3635
}
3736

37+
// @org.junit.Test
38+
public void hugeFileStreamTest() {
39+
byte[] hugeData = new byte[1000*1024*1024];
40+
new Random(1).nextBytes(hugeData);
41+
NamedStreamable.ByteArrayWrapper largeFile = new NamedStreamable.ByteArrayWrapper("massive.txt", hugeData);
42+
try {
43+
MerkleNode addResult = ipfs.add(largeFile);
44+
InputStream in = ipfs.catStream(addResult.hash);
45+
46+
byte[] res = new byte[hugeData.length];
47+
int offset = 0;
48+
byte[] buf = new byte[4096];
49+
int r;
50+
while ((r = in.read(buf)) >= 0) {
51+
try {
52+
System.arraycopy(buf, 0, res, offset, r);
53+
offset += r;
54+
}catch (Exception e){
55+
e.printStackTrace();
56+
}
57+
}
58+
if (!Arrays.equals(res, hugeData))
59+
throw new IllegalStateException("Different contents!");
60+
} catch (IOException e) {
61+
throw new RuntimeException(e);
62+
}
63+
}
64+
3865
@org.junit.Test
3966
public void hostFileTest() {
4067
NamedStreamable hostFile = new NamedStreamable.FileWrapper(new File("Makefile"));

0 commit comments

Comments
 (0)