Пример #1
0
 public LZ4EncoderStream(
     Stream inner,
     ILZ4FrameInfo frameInfo,
     Func <ILZ4FrameInfo, ILZ4Encoder> encoderFactory,
     bool leaveOpen = false)
 {
     _inner          = inner;
     _frameInfo      = frameInfo;
     _encoderFactory = encoderFactory;
     _leaveOpen      = leaveOpen;
 }
Пример #2
0
        private void ReadFrame()
        {
            Read0();

            var magic = TryRead32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            Read0();

            var FLG_BD = Read16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;

            var blockSizeCode = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Read64() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Read8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            _frameInfo = new LZ4FrameInfo(contentLength, contentChecksum, blockChaining, blockChecksum, blockSize);
            _decoder   = _decoderFactory(_frameInfo);
            _buffer    = new byte[blockSize];
        }
Пример #3
0
        private void CloseFrame()
        {
            if (_decoder == null)
            {
                return;
            }

            try
            {
                _frameInfo = null;
                _buffer    = null;

                // if you need any exceptions throw them here

                _decoder.Dispose();
            }
            finally
            {
                _decoder = null;
            }
        }