📄 encoder.java
字号:
}
}
}
}
}
}
}
}
boolean ChangePair(int smallDist, int bigDist)
{
int kDif = 7;
return (smallDist < (1 << (32 - kDif)) && bigDist >= (smallDist << kDif));
}
int GetOptimumFast(int position, int[] backRes) throws IOException
{
int lenMain;
if (!_longestMatchWasFound)
{
lenMain = ReadMatchDistances();
}
else
{
lenMain = _longestMatchLength;
_longestMatchWasFound = false;
}
int repMaxIndex = 0;
for (int i = 0; i < Base.kNumRepDistances; i++)
{
repLens[i] = _matchFinder.GetMatchLen(0 - 1, _repDistances[i], Base.kMatchMaxLen);
if (i == 0 || repLens[i] > repLens[repMaxIndex])
repMaxIndex = i;
}
if (repLens[repMaxIndex] >= _numFastBytes)
{
backRes[0] = repMaxIndex;
int lenRes = repLens[repMaxIndex];
MovePos(lenRes - 1);
return lenRes;
}
if (lenMain >= _numFastBytes)
{
backRes[0] = _matchDistances[_numFastBytes] + Base.kNumRepDistances;
MovePos(lenMain - 1);
return lenMain;
}
while (lenMain > 2)
{
if (!ChangePair(_matchDistances[lenMain - 1], _matchDistances[lenMain]))
break;
lenMain--;
}
if (lenMain == 2 && _matchDistances[2] >= 0x80)
lenMain = 1;
int backMain = _matchDistances[lenMain];
if (repLens[repMaxIndex] >= 2)
{
if (repLens[repMaxIndex] + 1 >= lenMain ||
repLens[repMaxIndex] + 2 >= lenMain && (backMain > (1 << 12)))
{
backRes[0] = repMaxIndex;
int lenRes = repLens[repMaxIndex];
MovePos(lenRes - 1);
return lenRes;
}
}
if (lenMain >= 2)
{
_longestMatchLength = ReadMatchDistances();
if (_longestMatchLength >= 2 &&
(
(_longestMatchLength >= lenMain &&
_matchDistances[lenMain] < backMain) ||
_longestMatchLength == lenMain + 1 &&
!ChangePair(backMain, _matchDistances[_longestMatchLength]) ||
_longestMatchLength > lenMain + 1 ||
_longestMatchLength + 1 >= lenMain && lenMain >= 3 &&
ChangePair(_matchDistances[lenMain - 1], backMain)
)
)
{
_longestMatchWasFound = true;
backRes[0] = -1;
return 1;
}
for (int i = 0; i < Base.kNumRepDistances; i++)
{
int repLen = _matchFinder.GetMatchLen(0 - 1, _repDistances[i], Base.kMatchMaxLen);
if (repLen >= 2 && repLen + 1 >= lenMain)
{
_longestMatchWasFound = true;
backRes[0] = -1;
return 1;
}
}
backRes[0] = backMain + Base.kNumRepDistances;
MovePos(lenMain - 2);
return lenMain;
}
backRes[0] = -1;
return 1;
}
void WriteEndMarker(int posState) throws IOException
{
if (!_writeEndMark)
return;
_rangeEncoder.Encode(_isMatch, (_state << Base.kNumPosStatesBitsMax) + posState, 1);
_rangeEncoder.Encode(_isRep, _state, 0);
_state = Base.StateUpdateMatch(_state);
int len = Base.kMatchMinLen; // kMatchMaxLen;
_lenEncoder.Encode(_rangeEncoder, len - Base.kMatchMinLen, posState);
int posSlot = (1 << Base.kNumPosSlotBits) - 1;
int lenToPosState = Base.GetLenToPosState(len);
_posSlotEncoder[lenToPosState].Encode(_rangeEncoder, posSlot);
int footerBits = 30;
int posReduced = (1 << footerBits) - 1;
_rangeEncoder.EncodeDirectBits(posReduced >> Base.kNumAlignBits, footerBits - Base.kNumAlignBits);
_posAlignEncoder.ReverseEncode(_rangeEncoder, posReduced & Base.kAlignMask);
}
void Flush(int nowPos) throws IOException
{
ReleaseMFStream();
WriteEndMarker(nowPos & _posStateMask);
_rangeEncoder.FlushData();
_rangeEncoder.FlushStream();
}
int[] posTemp = new int[1];
public void CodeOneBlock(long[] inSize, long[] outSize, boolean[] finished) throws IOException
{
inSize[0] = 0;
outSize[0] = 0;
finished[0] = true;
if (_inStream != null)
{
_matchFinder.SetStream(_inStream);
_matchFinder.Init();
_needReleaseMFStream = true;
_inStream = null;
}
if (_finished)
return;
_finished = true;
long progressPosValuePrev = nowPos64;
if (nowPos64 == 0)
{
if (_matchFinder.GetNumAvailableBytes() == 0)
{
Flush((int)nowPos64);
return;
}
ReadMatchDistances();
int posState = (int)(nowPos64) & _posStateMask;
_rangeEncoder.Encode(_isMatch, (_state << Base.kNumPosStatesBitsMax) + posState, 0);
_state = Base.StateUpdateChar(_state);
byte curByte = _matchFinder.GetIndexByte(0 - _additionalOffset);
_literalEncoder.GetSubCoder((int)(nowPos64), _previousByte).Encode(_rangeEncoder, curByte);
_previousByte = curByte;
_additionalOffset--;
nowPos64++;
}
if (_matchFinder.GetNumAvailableBytes() == 0)
{
Flush((int)nowPos64);
return;
}
while (true)
{
int posState = ((int)nowPos64) & _posStateMask;
int len;
if (_fastMode)
len = GetOptimumFast((int)nowPos64, posTemp);
else
len = GetOptimum((int)nowPos64, posTemp);
int pos = posTemp[0];
int complexState = (_state << Base.kNumPosStatesBitsMax) + posState;
if (len == 1 && pos == -1)
{
_rangeEncoder.Encode(_isMatch, complexState, 0);
byte curByte = _matchFinder.GetIndexByte((int)(0 - _additionalOffset));
LiteralEncoder.Encoder2 subCoder = _literalEncoder.GetSubCoder((int)nowPos64, _previousByte);
if (!Base.StateIsCharState(_state))
{
byte matchByte = _matchFinder.GetIndexByte((int)(0 - _repDistances[0] - 1 - _additionalOffset));
subCoder.EncodeMatched(_rangeEncoder, matchByte, curByte);
}
else
subCoder.Encode(_rangeEncoder, curByte);
_previousByte = curByte;
_state = Base.StateUpdateChar(_state);
}
else
{
_rangeEncoder.Encode(_isMatch, complexState, 1);
if (pos < Base.kNumRepDistances)
{
_rangeEncoder.Encode(_isRep, _state, 1);
if (pos == 0)
{
_rangeEncoder.Encode(_isRepG0, _state, 0);
if (len == 1)
_rangeEncoder.Encode(_isRep0Long, complexState, 0);
else
_rangeEncoder.Encode(_isRep0Long, complexState, 1);
}
else
{
_rangeEncoder.Encode(_isRepG0, _state, 1);
if (pos == 1)
_rangeEncoder.Encode(_isRepG1, _state, 0);
else
{
_rangeEncoder.Encode(_isRepG1, _state, 1);
_rangeEncoder.Encode(_isRepG2, _state, pos - 2);
}
}
if (len == 1)
_state = Base.StateUpdateShortRep(_state);
else
{
_repMatchLenEncoder.Encode(_rangeEncoder, len - Base.kMatchMinLen, posState);
_state = Base.StateUpdateRep(_state);
}
int distance = _repDistances[pos];
if (pos != 0)
{
for (int i = pos; i >= 1; i--)
_repDistances[i] = _repDistances[i - 1];
_repDistances[0] = distance;
}
}
else
{
_rangeEncoder.Encode(_isRep, _state, 0);
_state = Base.StateUpdateMatch(_state);
_lenEncoder.Encode(_rangeEncoder, len - Base.kMatchMinLen, posState);
pos -= Base.kNumRepDistances;
int posSlot = GetPosSlot(pos);
int lenToPosState = Base.GetLenToPosState(len);
_posSlotEncoder[lenToPosState].Encode(_rangeEncoder, posSlot);
if (posSlot >= Base.kStartPosModelIndex)
{
int footerBits = (int)((posSlot >> 1) - 1);
int baseVal = ((2 | (posSlot & 1)) << footerBits);
int posReduced = pos - baseVal;
if (posSlot < Base.kEndPosModelIndex)
BitTreeEncoder.ReverseEncode(_posEncoders,
baseVal - posSlot - 1, _rangeEncoder, footerBits, posReduced);
else
{
_rangeEncoder.EncodeDirectBits(posReduced >> Base.kNumAlignBits, footerBits - Base.kNumAlignBits);
_posAlignEncoder.ReverseEncode(_rangeEncoder, posReduced & Base.kAlignMask);
if (!_fastMode)
if (--_alignPriceCount == 0)
FillAlignPrices();
}
}
int distance = pos;
for (int i = Base.kNumRepDistances - 1; i >= 1; i--)
_repDistances[i] = _repDistances[i - 1];
_repDistances[0] = distance;
}
_previousByte = _matchFinder.GetIndexByte(len - 1 - _additionalOffset);
}
_additionalOffset -= len;
nowPos64 += len;
if (!_fastMode)
if (nowPos64 - lastPosSlotFillingPos >= (1 << 9))
{
FillPosSlotPrices();
FillDistancesPrices();
lastPosSlotFillingPos = nowPos64;
}
if (_additionalOffset == 0)
{
inSize[0] = nowPos64;
outSize[0] = _rangeEncoder.GetProcessedSizeAdd();
if (_matchFinder.GetNumAvailableBytes() == 0)
{
Flush((int)nowPos64);
return;
}
if (nowPos64 - progressPosValuePrev >= (1 << 12))
{
_finished = false;
finished[0] = false;
return;
}
}
}
}
void ReleaseMFStream()
{
if (_matchFinder != null && _needReleaseMFStream)
{
_matchFinder.ReleaseStream();
_needReleaseMFStream = false;
}
}
void SetOutStream(java.io.OutputStream outStream)
{ _rangeEncoder.SetStream(outStream); }
void ReleaseOutStream()
{ _rangeEncoder.ReleaseStream(); }
void ReleaseStreams()
{
ReleaseMFStream();
ReleaseOutStream();
}
void SetStreams(java.io.InputStream inStream, java.io.OutputStream outStream,
long inSize, long outSize)
{
_inStream = inStream;
_finished = false;
Create();
SetOutStream(outStream);
Init();
if (!_fastMode)
{
FillPosSlotPrices();
FillDistancesPrices();
FillAlignPrices();
}
_lenEncoder.SetTableSize(_numFastBytes + 1 - Base.kMatchMinLen);
_lenEncoder.UpdateTables(1 << _posStateBits);
_repMatchLenEncoder.SetTableSize(_numFastBytes + 1 - Base.kMatchMinLen);
_repMatchLenEncoder.UpdateTables(1 << _posStateBits);
lastPosSlotFillingPos = 0;
nowPos64 = 0;
}
long[] processedInSize = new long[1];
long[] processedOutSize = new long[1];
boolean[] finished = new boolean[1];
public void Code(java.io.InputStream inStream, java.io.OutputStream outStream,
long inSize, long outSize, ICodeProgress progress) throws IOException
{
_needReleaseMFStream = false;
try
{
SetStreams(inStream, outStream, inSize, outSize);
while (true)
{
CodeOneBlock(processedInSize, processedOutSize, finished);
if (finished[0])
return;
if (progress != null)
{
progress.SetProgress(processedInSize[0], processedOutSize[0]);
}
}
}
finally
{
ReleaseStreams();
}
}
public static final int kPropSize = 5;
byte[] properties = new byte[kPropSize];
public void WriteCoderProperties(java.io.OutputStream outStream) throws IOException
{
properties[0] = (byte)((_posStateBits * 5 + _numLiteralPosStateBits) * 9 + _numLiteralContextBits);
for (int i = 0; i < 4; i++)
properties[1 + i] = (byte)(_dictionarySize >> (8 * i));
outStream.write(properties, 0, kPropSize);
}
void FillPosSlotPrices()
{
for (int lenToPosState = 0; lenToPosState < Base.kNumLenToPosStates; lenToPosState++)
{
int posSlot;
for (posSlot = 0; posSlot < Base.kEndPosModelIndex && posSlot < _distTableSize; posSlot++)
_posSlotPrices[(posSlot << Base.kNumLenToPosStatesBits) + lenToPosState] = _posSlotEncoder[lenToPosState].GetPrice(posSlot);
for (; posSlot < _distTableSize; posSlot++)
_posSlotPrices[(posSlot << Base.kNumLenToPosStatesBits) + lenToPosState] = _posSlotEncoder[lenToPosState].GetPrice(posSlot) +
((((posSlot >> 1) - 1) - Base.kNumAlignBits) << SevenZip.Compression.RangeCoder.Encoder.kNumBitPriceShiftBits);
}
}
void FillDistancesPrices()
{
for (int lenToPosState = 0; lenToPosState < Base.kNumLenToPosStates; lenToPosState++)
{
int i;
for (i = 0; i < Base.kStartPosModelIndex; i++)
_distancesPrices[(i << Base.kNumLenToPosStatesBits) + lenToPosState] = _posSlotPrices[(i << Base.kNumLenToPosStatesBits) + lenToPosState];
for (; i < Base.kNumFullDistances; i++)
{
int posSlot = GetPosSlot(i);
int footerBits = ((posSlot >> 1) - 1);
int baseVal = ((2 | (posSlot & 1)) << footerBits);
_distancesPrices[(i << Base.kNumLenToPosStatesBits) + lenToPosState] = _posSlotPrices[(posSlot << Base.kNumLenToPosStatesBits) + lenToPosState] +
BitTreeEncoder.ReverseGetPrice(_posEncoders,
baseVal - posSlot - 1, footerBits, i - baseVal);
}
}
}
void FillAlignPrices()
{
for (int i = 0; i < Base.kAlignTableSize; i++)
_alignPrices[i] = _posAlignEncoder.ReverseGetPrice(i);
_alignPriceCount = Base.kAlignTableSize;
}
public boolean SetAlgorithm(int algorithm)
{
if (algorithm < 0 || algorithm > 2)
return false;
_fastMode = (algorithm == 0);
_maxMode = (algorithm >= 2);
return true;
}
public boolean SetDictionarySize(int dictionarySize)
{
int kDicLogSizeMaxCompress = Base.kDicLogSizeMax;
if (dictionarySize < (1 << Base.kDicLogSizeMin) || dictionarySize > (1 << kDicLogSizeMaxCompress))
return false;
_dictionarySize = dictionarySize;
int dicLogSize;
for (dicLogSize = 0; dictionarySize > (1 << dicLogSize); dicLogSize++);
_distTableSize = dicLogSize * 2;
return true;
}
public boolean SeNumFastBytes(int numFastBytes)
{
if (numFastBytes < 5 || numFastBytes > Base.kMatchMaxLen)
return false;
_numFastBytes = numFastBytes;
return true;
}
public boolean SetMatchFinder(int matchFinderIndex)
{
if (matchFinderIndex < 0 || matchFinderIndex > 2)
return false;
int matchFinderIndexPrev = _matchFinderType;
_matchFinderType = matchFinderIndex;
if (_matchFinder != null && matchFinderIndexPrev != _matchFinderType)
{
_dictionarySizePrev = -1;
_matchFinder = null;
}
return true;
}
public boolean SetLcLpPb(int lc, int lp, int pb)
{
if (
lp < 0 || lp > Base.kNumLitPosStatesBitsEncodingMax ||
lc < 0 || lc > Base.kNumLitContextBitsMax ||
pb < 0 || pb > Base.kNumPosStatesBitsEncodingMax)
return false;
_numLiteralPosStateBits = lp;
_numLiteralContextBits = lc;
_posStateBits = pb;
_posStateMask = ((1) << _posStateBits) - 1;
return true;
}
public void SetEndMarkerMode(boolean endMarkerMode)
{
_writeEndMark = endMarkerMode;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -