/// <summary> /// Creates a new instance of the <see cref="LZ4EncoderStream"/> class. /// </summary> /// <param name="inner">Inner stream.</param> /// <param name="descriptor">LZ4 descriptor.</param> /// <param name="encoderFactory">A function to return the appropriate encoder according to the <paramref name="descriptor"/>.</param> /// <param name="leaveOpen">Indicates whether <paramref name="inner"/> stream should be left open after disposing.</param> internal LZ4EncoderStream(Stream inner, ILZ4FrameDescriptor descriptor, Func<ILZ4FrameDescriptor, ILZ4Encoder> encoderFactory, bool leaveOpen = false) { _inner = inner; _descriptor = descriptor; _encoderFactory = encoderFactory; _leaveOpen = leaveOpen; }
private void ReadFrame() { Read0(); uint? magic = TryRead32(); if (magic != 0x184D2204) throw new InvalidDataException(RS.ExpectLZ4MagicNumber); Read0(); ushort flgBd = Read16(); int flg = flgBd & 0xFF; int bd = (flgBd >> 8) & 0xFF; int version = (flg >> 6) & 0x11; if (version != 1) throw new InvalidDataException(string.Format(RS.LZ4VersionNotSupported, version)); bool blockChaining = ((flg >> 5) & 0x01) == 0; bool blockChecksum = ((flg >> 4) & 0x01) != 0; bool hasContentSize = ((flg >> 3) & 0x01) != 0; bool contentChecksum = ((flg >> 2) & 0x01) != 0; bool hasDictionary = (flg & 0x01) != 0; int blockSizeCode = (bd >> 4) & 0x07; long? contentLength = hasContentSize ? (long?) Read64() : null; uint? dictionaryId = hasDictionary ? (uint?) Read32() : null; byte actualHC = (byte)(XXHash32.DigestOf(_buffer16, 0, _index16) >> 8); byte expectedHC = Read8(); if (actualHC != expectedHC) throw new InvalidDataException(RS.BadLZ4FrameHeaderChecksum); int blockSize = MaxBlockSize(blockSizeCode); if (hasDictionary) { // Write32(dictionaryId); throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Predefined Dictionaries", GetType().Name)); } _frameInfo = new LZ4FrameDescriptor(contentLength, contentChecksum, blockChaining, blockChecksum, dictionaryId, blockSize); _decoder = _decoderFactory(_frameInfo); _buffer = new byte[blockSize]; }
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; } }