package org.bson.json;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
class JsonStreamBuffer implements JsonBuffer {
private final Reader reader;
private final List<Integer> markedPositions = new ArrayList<Integer>();
private final int initialBufferSize;
private int position;
private int lastChar;
private boolean reuseLastChar;
private boolean eof;
private char[] buffer;
private int bufferStartPos;
private int bufferCount;
JsonStreamBuffer(final Reader reader) {
this(reader, 16);
}
JsonStreamBuffer(final Reader reader, final int initialBufferSize) {
this.initialBufferSize = initialBufferSize;
this.reader = reader;
resetBuffer();
}
public int getPosition() {
return position;
}
public int read() {
if (eof) {
throw new JsonParseException("Trying to read past EOF.");
}
if (reuseLastChar) {
reuseLastChar = false;
int reusedChar = lastChar;
lastChar = -1;
position++;
return reusedChar;
}
if (position - bufferStartPos < bufferCount) {
int currChar = buffer[position - bufferStartPos];
lastChar = currChar;
position++;
return currChar;
}
if (markedPositions.isEmpty()) {
resetBuffer();
}
try {
int nextChar = reader.read();
if (nextChar != -1) {
lastChar = nextChar;
addToBuffer((char) nextChar);
}
position++;
if (nextChar == -1) {
eof = true;
}
return nextChar;
} catch (final IOException e) {
throw new JsonParseException(e);
}
}
private void resetBuffer() {
bufferStartPos = -1;
bufferCount = 0;
buffer = new char[initialBufferSize];
}
public void unread(final int c) {
eof = false;
if (c != -1 && lastChar == c) {
reuseLastChar = true;
position--;
}
}
public int mark() {
if (bufferCount == 0) {
bufferStartPos = position;
}
if (!markedPositions.contains(position)) {
markedPositions.add(position);
}
return position;
}
public void reset(final int markPos) {
if (markPos > position) {
throw new IllegalStateException("mark cannot reset ahead of position, only back");
}
int idx = markedPositions.indexOf(markPos);
if (idx == -1) {
throw new IllegalArgumentException("mark invalidated");
}
if (markPos != position) {
reuseLastChar = false;
}
markedPositions.subList(idx, markedPositions.size()).clear();
position = markPos;
}
public void discard(final int markPos) {
int idx = markedPositions.indexOf(markPos);
if (idx == -1) {
return;
}
markedPositions.subList(idx, markedPositions.size()).clear();
}
private void addToBuffer(final char curChar) {
if (!markedPositions.isEmpty()) {
if (bufferCount == buffer.length) {
char[] newBuffer = new char[buffer.length * 2];
System.arraycopy(buffer, 0, newBuffer, 0, bufferCount);
buffer = newBuffer;
}
buffer[bufferCount] = curChar;
bufferCount++;
}
}
}