package org.glassfish.grizzly.http2.hpack;
import org.glassfish.grizzly.Buffer;
import java.util.Arrays;
final class StringReader {
private static final byte NEW = 0x0;
private static final byte FIRST_BYTE_READ = 0x1;
private static final byte LENGTH_READ = 0x2;
private static final byte DONE = 0x4;
private final IntegerReader intReader = new IntegerReader();
private final Huffman.Reader huffmanReader = new Huffman.Reader();
private final ISO_8859_1.Reader plainReader = new ISO_8859_1.Reader();
private byte state = NEW;
private boolean huffman;
private int remainingLength;
boolean read(Buffer input, Appendable output) {
if (state == DONE) {
return true;
}
if (!input.hasRemaining()) {
return false;
}
if (state == NEW) {
int p = input.position();
huffman = (input.get(p) & 0b10000000) != 0;
state = FIRST_BYTE_READ;
intReader.configure(7);
}
if (state == FIRST_BYTE_READ) {
boolean lengthRead = intReader.read(input);
if (!lengthRead) {
return false;
}
remainingLength = intReader.get();
state = LENGTH_READ;
}
if (state == LENGTH_READ) {
boolean isLast = input.remaining() >= remainingLength;
int oldLimit = input.limit();
if (isLast) {
input.limit(input.position() + remainingLength);
}
remainingLength -= Math.min(input.remaining(), remainingLength);
if (huffman) {
huffmanReader.read(input, output, isLast);
} else {
plainReader.read(input, output);
}
if (isLast) {
input.limit(oldLimit);
state = DONE;
}
return isLast;
}
throw new InternalError(Arrays.toString(
new Object[]{state, huffman, remainingLength}));
}
boolean isHuffmanEncoded() {
if (state < FIRST_BYTE_READ) {
throw new IllegalStateException("Has not been fully read yet");
}
return huffman;
}
void reset() {
if (huffman) {
huffmanReader.reset();
} else {
plainReader.reset();
}
intReader.reset();
state = NEW;
}
}