diff --git a/project/build.properties b/project/build.properties index 080a737..8522443 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.3.0 +sbt.version=1.3.2 diff --git a/src/main/java/net/woggioni/worth/buffer/LookAheadInputStream.java b/src/main/java/net/woggioni/worth/buffer/LookAheadInputStream.java index 6bf9342..d8a06a3 100644 --- a/src/main/java/net/woggioni/worth/buffer/LookAheadInputStream.java +++ b/src/main/java/net/woggioni/worth/buffer/LookAheadInputStream.java @@ -6,7 +6,10 @@ import java.io.InputStream; public class LookAheadInputStream extends InputStream { + private final byte[] buffer = new byte[1024]; private final InputStream stream; + private int bufferFill = -1; + private int cursor = -1; private int currentByte; public LookAheadInputStream(InputStream stream) { @@ -16,9 +19,18 @@ public class LookAheadInputStream extends InputStream { @Override @SneakyThrows public int read() { - int result = currentByte; - currentByte = stream.read(); - return result; + if (cursor > bufferFill) { + return -1; + } 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() { diff --git a/src/main/java/net/woggioni/worth/buffer/LookAheadTextInputStream.java b/src/main/java/net/woggioni/worth/buffer/LookAheadTextInputStream.java index 3cb4aec..0088ec8 100644 --- a/src/main/java/net/woggioni/worth/buffer/LookAheadTextInputStream.java +++ b/src/main/java/net/woggioni/worth/buffer/LookAheadTextInputStream.java @@ -7,8 +7,8 @@ import java.io.Reader; public class LookAheadTextInputStream extends InputStream { + private final char[] buffer = new char[1024]; private final Reader reader; - private char[] buffer = new char[1024]; private int bufferFill = -1; private int cursor = -1; private int currentChar; diff --git a/src/main/java/net/woggioni/worth/serialization/binary/JBONParser.java b/src/main/java/net/woggioni/worth/serialization/binary/JBONParser.java index 02e60cb..46781f1 100644 --- a/src/main/java/net/woggioni/worth/serialization/binary/JBONParser.java +++ b/src/main/java/net/woggioni/worth/serialization/binary/JBONParser.java @@ -4,7 +4,6 @@ import lombok.SneakyThrows; import net.woggioni.worth.buffer.LookAheadInputStream; import net.woggioni.worth.exception.ParseException; import net.woggioni.worth.serialization.ValueParser; -import net.woggioni.worth.serialization.json.JSONParser; import net.woggioni.worth.utils.Leb128; import net.woggioni.worth.utils.WorthUtils; import net.woggioni.worth.xface.Parser; @@ -36,7 +35,6 @@ public class JBONParser extends ValueParser { return result; } }; - stream.read(); try { Integer currentId = null; @@ -132,7 +130,7 @@ public class JBONParser extends ValueParser { } 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) { throw error(ParseException::new, e.getMessage()); } finally {