package org.glassfish.grizzly.compression.lzma.impl.lz;
import org.glassfish.grizzly.Buffer;
import java.io.IOException;
public class InWindow {
public byte[] _bufferBase;
Buffer _buffer;
int _posLimit;
boolean _streamEndWasReached;
int _pointerToLastSafePosition;
public int _bufferOffset;
public int _blockSize;
public int _pos;
int _keepSizeBefore;
int _keepSizeAfter;
public int _streamPos;
public void moveBlock() {
int offset = _bufferOffset + _pos - _keepSizeBefore;
if (offset > 0) {
offset--;
}
int numBytes = _bufferOffset + _streamPos - offset;
System.arraycopy(_bufferBase, offset, _bufferBase, 0, numBytes);
_bufferOffset -= offset;
}
public void readBlock() throws IOException {
if (_streamEndWasReached) {
return;
}
while (true) {
int size = (0 - _bufferOffset) + _blockSize - _streamPos;
if (size == 0) {
return;
}
int pos = _buffer.position();
size = Math.min(size, _buffer.remaining());
_buffer.get(_bufferBase, _bufferOffset + _streamPos, size);
int numReadBytes = _buffer.position() - pos;
if (numReadBytes == 0) {
_posLimit = _streamPos;
int pointerToPostion = _bufferOffset + _posLimit;
if (pointerToPostion > _pointerToLastSafePosition) {
_posLimit = _pointerToLastSafePosition - _bufferOffset;
}
_streamEndWasReached = true;
return;
}
_streamPos += numReadBytes;
if (_streamPos >= _pos + _keepSizeAfter) {
_posLimit = _streamPos - _keepSizeAfter;
}
}
}
void free() {
_bufferBase = null;
}
public void create(int keepSizeBefore, int keepSizeAfter, int keepSizeReserv) {
_keepSizeBefore = keepSizeBefore;
_keepSizeAfter = keepSizeAfter;
int blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
if (_bufferBase == null || _blockSize != blockSize) {
free();
_blockSize = blockSize;
_bufferBase = new byte[_blockSize];
}
_pointerToLastSafePosition = _blockSize - keepSizeAfter;
}
public void setBuffer(Buffer buffer) {
_buffer = buffer;
}
public void releaseBuffer() {
_buffer = null;
}
public void init() throws IOException {
_bufferOffset = 0;
_pos = 0;
_streamPos = 0;
_streamEndWasReached = false;
readBlock();
}
public void movePos() throws IOException {
_pos++;
if (_pos > _posLimit) {
int pointerToPostion = _bufferOffset + _pos;
if (pointerToPostion > _pointerToLastSafePosition) {
moveBlock();
}
readBlock();
}
}
public byte getIndexByte(int index) {
return _bufferBase[_bufferOffset + _pos + index];
}
public int getMatchLen(int index, int distance, int limit) {
if (_streamEndWasReached) {
if ((_pos + index) + limit > _streamPos) {
limit = _streamPos - (_pos + index);
}
}
distance++;
int pby = _bufferOffset + _pos + index;
int i;
for (i = 0; i < limit && _bufferBase[pby + i] == _bufferBase[pby + i - distance]; i++);
return i;
}
public int getNumAvailableBytes() {
return _streamPos - _pos;
}
public void reduceOffsets(int subValue) {
_bufferOffset += subValue;
_posLimit -= subValue;
_pos -= subValue;
_streamPos -= subValue;
}
}