📄 encoder.java
字号:
}
}
}
if (newLen > numAvailableBytes)
{
newLen = numAvailableBytes;
for (numDistancePairs = 0; newLen > _matchDistances[numDistancePairs]; numDistancePairs += 2) ;
_matchDistances[numDistancePairs] = newLen;
numDistancePairs += 2;
}
if (newLen >= startLen)
{
normalMatchPrice = matchPrice + SevenZip.Compression.RangeCoder.Encoder.GetPrice0(_isRep[state]);
while (lenEnd < cur + newLen)
_optimum[++lenEnd].Price = kIfinityPrice;
int offs = 0;
while (startLen > _matchDistances[offs])
offs += 2;
for (int lenTest = startLen; ; lenTest++)
{
int curBack = _matchDistances[offs + 1];
int curAndLenPrice = normalMatchPrice + GetPosLenPrice(curBack, lenTest, posState);
Optimal optimum = _optimum[cur + lenTest];
if (curAndLenPrice < optimum.Price)
{
optimum.Price = curAndLenPrice;
optimum.PosPrev = cur;
optimum.BackPrev = curBack + Base.kNumRepDistances;
optimum.Prev1IsChar = false;
}
if (lenTest == _matchDistances[offs])
{
if (lenTest < numAvailableBytesFull)
{
int t = Math.min(numAvailableBytesFull - 1 - lenTest, _numFastBytes);
int lenTest2 = _matchFinder.GetMatchLen(lenTest, curBack, t);
if (lenTest2 >= 2)
{
int state2 = Base.StateUpdateMatch(state);
int posStateNext = (position + lenTest) & _posStateMask;
int curAndLenCharPrice = curAndLenPrice +
SevenZip.Compression.RangeCoder.Encoder.GetPrice0(_isMatch[(state2 << Base.kNumPosStatesBitsMax) + posStateNext]) +
_literalEncoder.GetSubCoder(position + lenTest,
_matchFinder.GetIndexByte(lenTest - 1 - 1)).
GetPrice(true,
_matchFinder.GetIndexByte(lenTest - (curBack + 1) - 1),
_matchFinder.GetIndexByte(lenTest - 1));
state2 = Base.StateUpdateChar(state2);
posStateNext = (position + lenTest + 1) & _posStateMask;
int nextMatchPrice = curAndLenCharPrice + SevenZip.Compression.RangeCoder.Encoder.GetPrice1(_isMatch[(state2 << Base.kNumPosStatesBitsMax) + posStateNext]);
int nextRepMatchPrice = nextMatchPrice + SevenZip.Compression.RangeCoder.Encoder.GetPrice1(_isRep[state2]);
int offset = lenTest + 1 + lenTest2;
while (lenEnd < cur + offset)
_optimum[++lenEnd].Price = kIfinityPrice;
curAndLenPrice = nextRepMatchPrice + GetRepPrice(0, lenTest2, state2, posStateNext);
optimum = _optimum[cur + offset];
if (curAndLenPrice < optimum.Price)
{
optimum.Price = curAndLenPrice;
optimum.PosPrev = cur + lenTest + 1;
optimum.BackPrev = 0;
optimum.Prev1IsChar = true;
optimum.Prev2 = true;
optimum.PosPrev2 = cur;
optimum.BackPrev2 = curBack + Base.kNumRepDistances;
}
}
}
offs += 2;
if (offs == numDistancePairs)
break;
}
}
}
}
}
boolean ChangePair(int smallDist, int bigDist)
{
int kDif = 7;
return (smallDist < (1 << (32 - kDif)) && bigDist >= (smallDist << kDif));
}
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;
_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();
}
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 len = GetOptimum((int)nowPos64);
int pos = backRes;
int posState = ((int)nowPos64) & _posStateMask;
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);
_alignPriceCount++;
}
}
int distance = pos;
for (int i = Base.kNumRepDistances - 1; i >= 1; i--)
_repDistances[i] = _repDistances[i - 1];
_repDistances[0] = distance;
_matchPriceCount++;
}
_previousByte = _matchFinder.GetIndexByte(len - 1 - _additionalOffset);
}
_additionalOffset -= len;
nowPos64 += len;
if (_additionalOffset == 0)
{
// if (!_fastMode)
if (_matchPriceCount >= (1 << 7))
FillDistancesPrices();
if (_alignPriceCount >= Base.kAlignTableSize)
FillAlignPrices();
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)
{
FillDistancesPrices();
FillAlignPrices();
}
_lenEncoder.SetTableSize(_numFastBytes + 1 - Base.kMatchMinLen);
_lenEncoder.UpdateTables(1 << _posStateBits);
_repMatchLenEncoder.SetTableSize(_numFastBytes + 1 - Base.kMatchMinLen);
_repMatchLenEncoder.UpdateTables(1 << _posStateBits);
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);
}
int[] tempPrices = new int[Base.kNumFullDistances];
int _matchPriceCount;
void FillDistancesPrices()
{
for (int i = Base.kStartPosModelIndex; i < Base.kNumFullDistances; i++)
{
int posSlot = GetPosSlot(i);
int footerBits = (int)((posSlot >> 1) - 1);
int baseVal = ((2 | (posSlot & 1)) << footerBits);
tempPrices[i] = BitTreeEncoder.ReverseGetPrice(_posEncoders,
baseVal - posSlot - 1, footerBits, i - baseVal);
}
for (int lenToPosState = 0; lenToPosState < Base.kNumLenToPosStates; lenToPosState++)
{
int posSlot;
BitTreeEncoder encoder = _posSlotEncoder[lenToPosState];
int st = (lenToPosState << Base.kNumPosSlotBits);
for (posSlot = 0; posSlot < _distTableSize; posSlot++)
_posSlotPrices[st + posSlot] = encoder.GetPrice(posSlot);
for (posSlot = Base.kEndPosModelIndex; posSlot < _distTableSize; posSlot++)
_posSlotPrices[st + posSlot] += ((((posSlot >> 1) - 1) - Base.kNumAlignBits) << SevenZip.Compression.RangeCoder.Encoder.kNumBitPriceShiftBits);
int st2 = lenToPosState * Base.kNumFullDistances;
int i;
for (i = 0; i < Base.kStartPosModelIndex; i++)
_distancesPrices[st2 + i] = _posSlotPrices[st + i];
for (; i < Base.kNumFullDistances; i++)
_distancesPrices[st2 + i] = _posSlotPrices[st + GetPosSlot(i)] + tempPrices[i];
}
_matchPriceCount = 0;
}
void FillAlignPrices()
{
for (int i = 0; i < Base.kAlignTableSize; i++)
_alignPrices[i] = _posAlignEncoder.ReverseGetPrice(i);
_alignPriceCount = 0;
}
public boolean SetAlgorithm(int algorithm)
{
/*
_fastMode = (algorithm == 0);
_maxMode = (algorithm >= 2);
*/
return true;
}
public boolean SetDictionarySize(int dictionarySize)
{
int kDicLogSizeMaxCompress = 29;
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 SetNumFastBytes(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 + -