File tree Expand file tree Collapse file tree 3 files changed +44
-1
lines changed
test/java/convex/core/data Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
1
package convex .core .data ;
2
2
3
+ import java .io .ByteArrayOutputStream ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStream ;
3
6
import java .util .Random ;
4
7
5
8
import org .bouncycastle .util .Arrays ;
@@ -69,6 +72,23 @@ public static ABlob fromHex(AString a) {
69
72
return fromHex (a .toString ());
70
73
}
71
74
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
+
72
92
/**
73
93
* Best effort attempt to parse a Blob. Must parse as a Blob of correct length
74
94
* @param o Object expected to contain a Blob value
Original file line number Diff line number Diff line change @@ -723,7 +723,13 @@ public static String readResourceAsString(String path) throws IOException {
723
723
}
724
724
}
725
725
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
+ */
727
733
public static InputStream getResourceAsStream (String path ) throws IOException {
728
734
InputStream inputStream = Utils .class .getResourceAsStream (path );
729
735
if (inputStream == null ) throw new IOException ("Resource not found: " + path );
Original file line number Diff line number Diff line change 9
9
import static org .junit .jupiter .api .Assertions .assertThrows ;
10
10
import static org .junit .jupiter .api .Assertions .assertTrue ;
11
11
12
+ import java .io .ByteArrayInputStream ;
12
13
import java .io .IOException ;
13
14
import java .nio .ByteBuffer ;
14
15
import java .util .Random ;
@@ -656,6 +657,22 @@ public void testBlobParse() {
656
657
assertEquals (b ,Blobs .parse (" 0x" +hex +" " ));
657
658
assertEquals (b ,Blobs .parse (" " +hex +" " ));
658
659
}
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
+ }
659
676
660
677
@ Test
661
678
public void testBlobEncoding () throws BadFormatException {
You can’t perform that action at this time.
0 commit comments