Skip to content

Commit

Permalink
#42 - Fixed stream issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
vzurauskas committed Jul 29, 2020
1 parent f8b5863 commit 41ac18b
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.vzurauskas.nereides.javax;

import java.io.IOException;
import java.io.InputStream;

public final class AutoResetInputStream extends InputStream {

private final InputStream origin;

public AutoResetInputStream(InputStream origin) {
super();
origin.mark(1 << 24);
this.origin = origin;
}

@Override
public void close() throws IOException {
super.close();
origin.reset();
}

@Override
public int read() throws IOException {
return origin.read();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/vzurauskas/nereides/javax/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class ByteArray {
while (true) {
int size = stream.read(data, 0, data.length);
if (size == -1) {
stream.close();
break;
}
output.write(data, 0, size);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/vzurauskas/nereides/javax/Cached.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.vzurauskas.nereides.javax;

import java.util.function.Supplier;

final class Cached<T> {
private final Supplier<T> scalar;
private T value;

Cached(Supplier<T> scalar) {
this.scalar = scalar;
}

public T value() {
if (value == null) {
value = scalar.get();
}
return value;
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/vzurauskas/nereides/javax/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ public Of(String string) {
* @param bytes JSON represented by an array of bytes.
*/
public Of(byte[] bytes) {
this(new ByteArrayInputStream(bytes));
this(
new AutoResetInputStream(
new ByteArrayInputStream(bytes)
)
);
}

/**
Expand All @@ -110,12 +114,20 @@ public Of(InputStream stream) {
*/
public Of(Path path) {
this(
() -> new Unchecked<>(
() -> new ByteArrayInputStream(Files.readAllBytes(path))
).value()
new Cached<>(
() -> new Unchecked<>(
() -> new AutoResetInputStream(
new ByteArrayInputStream(Files.readAllBytes(path))
)
).value()
)
);
}

private Of(Cached<InputStream> cached) {
this(cached::value);
}

private Of(Json json) {
this.origin = json;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/vzurauskas/nereides/javax/SmartJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public final class SmartJson implements Json {

private final Json origin;
private final Unchecked<JsonStructure> structure;
private final Cached<JsonStructure> structure;

/**
* Constructor.
Expand All @@ -41,13 +41,15 @@ public final class SmartJson implements Json {
public SmartJson(Json origin) {
this(
origin,
new Unchecked<>(
() -> javax.json.Json.createReader(origin.bytes()).read()
new Cached<>(
() -> new Unchecked<>(
() -> javax.json.Json.createReader(origin.bytes()).read()
).value()
)
);
}

private SmartJson(Json origin, Unchecked<JsonStructure> structure) {
private SmartJson(Json origin, Cached<JsonStructure> structure) {
this.origin = origin;
this.structure = structure;
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/vzurauskas/nereides/javax/JsonOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -95,4 +97,25 @@ void toStringWorksEvenIfMalformed() {
new Json.Of("malformed").toString()
);
}

@Test
void canReadTwice() {
String string = "{\"number\": 12}";
Json json = new Json.Of(string);
assertArrayEquals(string.getBytes(), new ByteArray(json).value());
assertArrayEquals(string.getBytes(), new ByteArray(json).value());
}

@Test
void doesntReadFileEachTimeJsonIsAccessed() throws IOException {
String string = "{\"number\": 12}";
File file = File.createTempFile("whatever", "whatever");
try (PrintStream ps = new PrintStream(file)) {
ps.print(string);
}
Json json = new Json.Of(file.toPath());
assertArrayEquals(string.getBytes(), new ByteArray(json).value());
file.delete();
assertArrayEquals(string.getBytes(), new ByteArray(json).value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,13 @@ void toStringWorksOfMalformed() {
new SmartJson(new Json.Of("malformed")).toString()
);
}

@Test
void canReadTwice() {
SmartJson json = new SmartJson(
new Json.Of("{\"field1\":\"value1\",\"field2\":\"value2\"}")
);
assertEquals("value1", json.leaf("field1"));
assertEquals("value1", json.leaf("field1"));
}
}

0 comments on commit 41ac18b

Please sign in to comment.