summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java b/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
new file mode 100644
index 00000000000..8eecc0d50f2
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
@@ -0,0 +1,83 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.slime;
+
+final class BufferedInput {
+
+ private final byte[] source;
+ private final int end;
+ private final int start;
+ private int position;
+ private String failReason;
+ private int failPos;
+
+ void fail(String reason) {
+ if (failed()) {
+ return;
+ }
+ failReason = reason;
+ failPos = position;
+ position = end;
+ }
+
+ public BufferedInput(byte[] bytes) {
+ this(bytes, 0, bytes.length);
+ }
+
+ public BufferedInput(byte[] bytes, int offset, int length) {
+ this.source = bytes;
+ this.start = offset;
+ position = offset;
+ this.end = offset + length;
+ }
+ public final byte getByte() {
+ if (position == end) {
+ fail("underflow");
+ return 0;
+ }
+ return source[position++];
+ }
+
+ public boolean failed() {
+ return failReason != null;
+ }
+
+ public boolean eof() {
+ return this.position == this.end;
+ }
+
+ public String getErrorMessage() {
+ return failReason;
+ }
+
+ public int getConsumedSize() {
+ return failed() ? 0 : position - start;
+ }
+
+ public byte[] getOffending() {
+ byte[] ret = new byte[failPos-start];
+ System.arraycopy(source, start, ret, 0, failPos-start);
+ return ret;
+ }
+
+ public final byte [] getBacking() { return source; }
+ public final int getPosition() { return position; }
+ public final void skip(int size) {
+ if (position + size > end) {
+ fail("underflow");
+ } else {
+ position += size;
+ }
+ }
+
+ public final byte[] getBytes(int size) {
+ if (position + size > end) {
+ fail("underflow");
+ return new byte[0];
+ }
+ byte[] ret = new byte[size];
+ for (int i = 0; i < size; i++) {
+ ret[i] = source[position++];
+ }
+ return ret;
+ }
+}