blob: ddbb25196b5600836311c8f5ddc47f2637e46ea5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// Copyright Yahoo. 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;
}
BufferedInput(byte[] bytes) {
this(bytes, 0, bytes.length);
}
BufferedInput(byte[] bytes, int offset, int length) {
this.source = bytes;
this.start = offset;
position = offset;
this.end = offset + length;
}
byte getByte() {
if (position == end) {
fail("underflow");
return 0;
}
return source[position++];
}
boolean failed() {
return failReason != null;
}
boolean eof() {
return this.position == this.end;
}
String getErrorMessage() {
return failReason;
}
int getConsumedSize() {
return failed() ? 0 : position - start;
}
byte[] getOffending() {
byte[] ret = new byte[failPos-start];
System.arraycopy(source, start, ret, 0, failPos-start);
return ret;
}
byte [] getBacking() { return source; }
int getPosition() { return position; }
void skip(int size) {
if (position + size > end) {
fail("underflow");
} else {
position += size;
}
}
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;
}
}
|