Skip to content

Commit b7c9653

Browse files
committed
Add Blobs.fromStream()
1 parent cd7a4e8 commit b7c9653

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

convex-core/src/main/java/convex/core/data/Blobs.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package convex.core.data;
22

3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
36
import java.util.Random;
47

58
import org.bouncycastle.util.Arrays;
@@ -69,6 +72,23 @@ public static ABlob fromHex(AString a) {
6972
return fromHex(a.toString());
7073
}
7174

75+
/**
76+
* Reads an InputStream as a canonical Blob.
77+
*
78+
* @param inputStream Stream of data to read as UTF-8 string
79+
* @return Blob content of stream
80+
* @throws IOException
81+
*/
82+
public static ABlob fromStream(InputStream inputStream) throws IOException {
83+
ABlob result=Blob.EMPTY;
84+
while (true) {
85+
byte[] bs=inputStream.readNBytes(Blob.CHUNK_LENGTH);
86+
if (bs.length==0) break;
87+
result=result.append(Blob.wrap(bs));
88+
}
89+
return result;
90+
}
91+
7292
/**
7393
* Best effort attempt to parse a Blob. Must parse as a Blob of correct length
7494
* @param o Object expected to contain a Blob value

convex-core/src/main/java/convex/core/util/Utils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,13 @@ public static String readResourceAsString(String path) throws IOException {
723723
}
724724
}
725725

726-
726+
/**
727+
* Gets a resource as a Stream.
728+
*
729+
* @param path Path to resource, e.g "/actors/token.cvx"
730+
* @return String content of resource file
731+
* @throws IOException If an IO error occurs
732+
*/
727733
public static InputStream getResourceAsStream(String path) throws IOException {
728734
InputStream inputStream = Utils.class.getResourceAsStream(path);
729735
if (inputStream == null) throw new IOException("Resource not found: " + path);

convex-core/src/test/java/convex/core/data/BlobsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.junit.jupiter.api.Assertions.assertThrows;
1010
import static org.junit.jupiter.api.Assertions.assertTrue;
1111

12+
import java.io.ByteArrayInputStream;
1213
import java.io.IOException;
1314
import java.nio.ByteBuffer;
1415
import java.util.Random;
@@ -656,6 +657,22 @@ public void testBlobParse() {
656657
assertEquals(b,Blobs.parse(" 0x"+hex+" "));
657658
assertEquals(b,Blobs.parse(" "+hex+" "));
658659
}
660+
661+
@Test
662+
public void testBlobFromStream() throws IOException {
663+
doBlobStreamTest(Blobs.empty());
664+
doBlobStreamTest(Samples.SMALL_BLOB);
665+
666+
doBlobStreamTest(Samples.FULL_BLOB);
667+
doBlobStreamTest(Samples.FULL_BLOB_PLUS);
668+
}
669+
670+
private void doBlobStreamTest(ABlob b) throws IOException {
671+
byte[] bs=b.getBytes();
672+
ByteArrayInputStream bis=new ByteArrayInputStream(bs);
673+
ABlob r=Blobs.fromStream(bis);
674+
assertEquals(b,r);
675+
}
659676

660677
@Test
661678
public void testBlobEncoding() throws BadFormatException {

0 commit comments

Comments
 (0)