Skip to content

Commit 45c8fc6

Browse files
committed
Add Reader support for InputStreams
1 parent b85cf3c commit 45c8fc6

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

convex-core/src/main/java/convex/core/json/JSON5Reader.java

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

33
import java.io.IOException;
4+
import java.io.InputStream;
45
import java.util.ArrayList;
56

67
import org.antlr.v4.runtime.CharStream;
@@ -135,6 +136,10 @@ public static ACell read(java.io.Reader r) throws IOException {
135136
return read(CharStreams.fromReader(r));
136137
}
137138

139+
public static ACell read(InputStream is) throws IOException {
140+
return read(CharStreams.fromStream(is));
141+
}
142+
138143
private static final ConvexErrorListener ERROR_LISTENER=new ConvexErrorListener();
139144

140145

convex-core/src/main/java/convex/core/json/JSONReader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.core.json;
22

33
import java.io.IOException;
4+
import java.io.InputStream;
45
import java.util.ArrayList;
56

67
import org.antlr.v4.runtime.CharStream;
@@ -132,6 +133,11 @@ public static ACell read(java.io.Reader r) throws IOException {
132133
return read(CharStreams.fromReader(r));
133134
}
134135

136+
public static ACell read(InputStream is) throws IOException {
137+
return read(CharStreams.fromStream(is));
138+
}
139+
140+
135141
private static final ConvexErrorListener ERROR_LISTENER=new ConvexErrorListener();
136142

137143

convex-core/src/main/java/convex/core/lang/Reader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.core.lang;
22

33
import java.io.IOException;
4+
import java.io.InputStream;
45

56
import convex.core.cvm.Syntax;
67
import convex.core.data.ACell;
@@ -59,6 +60,10 @@ public static ACell read(java.io.Reader source) throws IOException {
5960
return AntlrReader.read(source);
6061
}
6162

63+
public static ACell read(InputStream stream) throws IOException {
64+
return AntlrReader.read(stream);
65+
}
66+
6267
/**
6368
* Parses an expression and returns a canonical Cell representing a form
6469
*
@@ -70,4 +75,6 @@ public static <R extends ACell> R read(String source) {
7075
return (R) AntlrReader.read(source);
7176
}
7277

78+
79+
7380
}

convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.core.lang.reader;
22

33
import java.io.IOException;
4+
import java.io.InputStream;
45
import java.util.ArrayList;
56

67
import org.antlr.v4.runtime.CharStream;
@@ -387,6 +388,11 @@ public void enterAllForms(AllFormsContext ctx) {
387388
pushList();
388389
}
389390
}
391+
392+
public static ACell read(InputStream is) throws IOException {
393+
return read(CharStreams.fromStream(is));
394+
}
395+
390396

391397
public static ACell read(String s) {
392398
return read(CharStreams.fromString(s));

convex-core/src/test/java/convex/core/lang/ReaderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import static org.junit.jupiter.api.Assertions.assertSame;
99
import static org.junit.jupiter.api.Assertions.assertThrows;
1010

11+
import java.io.ByteArrayInputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.nio.charset.StandardCharsets;
15+
1116
import org.junit.jupiter.api.Test;
1217

1318
import convex.core.Result;
@@ -427,6 +432,15 @@ public void doIdempotencyTest(ACell cell) {
427432
doReadPrintTest("^{} 0xa89e59cc8ab9fc6a13785a37938c85b306b24663415effc01063a6e25ef52ebcd3647d3a77e0a33908a372146fdccab6");
428433
}
429434

435+
/**
436+
* Test cases that should read and print identically
437+
* @throws IOException
438+
*/
439+
@Test public void testReadFromStream() throws IOException {
440+
InputStream stream = new ByteArrayInputStream("[1 2 3]".getBytes(StandardCharsets.UTF_8));
441+
assertEquals(Vectors.of(1,2,3),Reader.read(stream));
442+
}
443+
430444
/**
431445
* Test cases for strings with Java escapes
432446
*/

0 commit comments

Comments
 (0)