added buffering to LookAheadInputStream

This commit is contained in:
2019-09-29 16:46:45 +01:00
parent 51a7e8ffb0
commit 3846a723cc
4 changed files with 18 additions and 8 deletions

View File

@@ -1 +1 @@
sbt.version=1.3.0 sbt.version=1.3.2

View File

@@ -6,7 +6,10 @@ import java.io.InputStream;
public class LookAheadInputStream extends InputStream { public class LookAheadInputStream extends InputStream {
private final byte[] buffer = new byte[1024];
private final InputStream stream; private final InputStream stream;
private int bufferFill = -1;
private int cursor = -1;
private int currentByte; private int currentByte;
public LookAheadInputStream(InputStream stream) { public LookAheadInputStream(InputStream stream) {
@@ -16,9 +19,18 @@ public class LookAheadInputStream extends InputStream {
@Override @Override
@SneakyThrows @SneakyThrows
public int read() { public int read() {
int result = currentByte; if (cursor > bufferFill) {
currentByte = stream.read(); return -1;
return result; } else if (cursor == bufferFill) {
do {
bufferFill = stream.read(buffer, 0, buffer.length) - 1;
cursor = 0;
} while(bufferFill == -1);
currentByte = bufferFill == -2 ? -1 : Math.floorMod(buffer[0], 256);
} else {
currentByte = Math.floorMod(buffer[++cursor], 256);
}
return currentByte;
} }
public int getCurrentByte() { public int getCurrentByte() {

View File

@@ -7,8 +7,8 @@ import java.io.Reader;
public class LookAheadTextInputStream extends InputStream { public class LookAheadTextInputStream extends InputStream {
private final char[] buffer = new char[1024];
private final Reader reader; private final Reader reader;
private char[] buffer = new char[1024];
private int bufferFill = -1; private int bufferFill = -1;
private int cursor = -1; private int cursor = -1;
private int currentChar; private int currentChar;

View File

@@ -4,7 +4,6 @@ import lombok.SneakyThrows;
import net.woggioni.worth.buffer.LookAheadInputStream; import net.woggioni.worth.buffer.LookAheadInputStream;
import net.woggioni.worth.exception.ParseException; import net.woggioni.worth.exception.ParseException;
import net.woggioni.worth.serialization.ValueParser; import net.woggioni.worth.serialization.ValueParser;
import net.woggioni.worth.serialization.json.JSONParser;
import net.woggioni.worth.utils.Leb128; import net.woggioni.worth.utils.Leb128;
import net.woggioni.worth.utils.WorthUtils; import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.xface.Parser; import net.woggioni.worth.xface.Parser;
@@ -36,7 +35,6 @@ public class JBONParser extends ValueParser {
return result; return result;
} }
}; };
stream.read();
try { try {
Integer currentId = null; Integer currentId = null;
@@ -132,7 +130,7 @@ public class JBONParser extends ValueParser {
} }
throw error(ParseException::new, "Unfinished %s", type); throw error(ParseException::new, "Unfinished %s", type);
} }
return WorthUtils.dynamicCast(stack.getFirst(), ArrayStackLevel.class).value.get(0); return ((ArrayStackLevel) stack.getFirst()).value.get(0);
} catch (NumberFormatException | NegativeArraySizeException e) { } catch (NumberFormatException | NegativeArraySizeException e) {
throw error(ParseException::new, e.getMessage()); throw error(ParseException::new, e.getMessage());
} finally { } finally {