/// <summary> /// Initializes a new instance of the <see cref="QuickLZCompressionStream"/> class. /// </summary> /// <param name="targetStream">The target stream.</param> /// <param name="writeBuffer">The write buffer.</param> /// <param name="compressionBuffer">The compression buffer.</param> public QuickLZCompressionStream(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer) { _quickLZ = Pool.Default.Alloc(); _targetStream = targetStream; _writeBuffer = writeBuffer; _compressedBuffer = compressionBuffer; }
/// <summary> /// Initializes a new instance of the <see cref="QuickLZCompressionStream"/> class. /// </summary> /// <param name="targetStream">The target stream.</param> /// <param name="writeBuffer">The write buffer.</param> /// <param name="compressionBuffer">The compression buffer.</param> public QuickLZCompressionStream(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer) { _quickLZ = Pool.Default.Alloc(); _targetStream = targetStream; _writeBuffer = writeBuffer; _compressedBuffer = compressionBuffer; }
/// <summary> /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// </summary> /// <exception cref="T:System.IO.IOException"> /// An I/O error occurs. /// </exception> public override void Flush() { if (_writeBufferOffset > 0) { //int compressedLength = _quickLZ.compress(_writeBuffer, _compressedBuffer, _writeBufferOffset); byte[] buffer = PersistenceList.Tools.ArrayCopy(_writeBuffer, 0, _writeBufferOffset); var _compressedBuffer = QuickLZ.compress(buffer, 3); _targetStream.Write(_compressedBuffer, 0, _compressedBuffer.Length); _writeBufferOffset = 0; } }
/// <summary> /// Ensures the unpacked buffer has enough space. /// </summary> /// <param name="packedBuffer">The packed buffer.</param> private void EnsureUnpackedBuffer(byte[] packedBuffer) { //int unpackedLength = _quickLZ.sizeDecompressed(packedBuffer); int unpackedLength = QuickLZ.sizeDecompressed(packedBuffer); if (_unpackedBuffer == null || _unpackedBuffer.Length < unpackedLength) { if (_unpackedBuffer != null) { Free(_unpackedBuffer); } _unpackedBuffer = Alloc(unpackedLength); } }
/// <summary> Initializes a new instance of the QuickLZStream class. </summary> /// <param name="stream"> Stream to compress or decompress. </param> /// <param name="mode"> The compression mode. </param> /// <param name="level"> The compression level (1-3). </param> /// <param name="bufferSize"> (optional) size of the buffer. </param> public QuickLZStream(Stream stream, CompressionMode mode, int level, int bufferSize = 1 << 20) { _quickLZ = new QuickLZ(level, true); _stream = stream; _compressionMode = mode; if (mode == CompressionMode.Decompress) { Fill(); } else { _writeBuffer = new byte[bufferSize]; _compressedBuffer = new byte[bufferSize + 400]; } }
/// <summary> Initializes a new instance of the QuickLZStream class. </summary> /// <param name="stream"> Stream to compress or decompress. </param> /// <param name="mode"> The compression mode. </param> /// <param name="level"> The compression level (1-3). </param> /// <param name="bufferSize"> (optional) size of the buffer. </param> public QuickLZStream(Stream stream, CompressionMode mode, int level, int bufferSize = 1 << 20) { _quickLZ = new QuickLZ(level, true); _stream = stream; _compressionMode = mode; if (mode == CompressionMode.Decompress) { Fill(); } else { _writeBuffer = new byte[bufferSize]; _compressedBuffer = new byte[bufferSize + 400]; } }
/// <summary> /// Fills or refills the read buffer. /// </summary> private void Fill() { int headerLength = _sourceStream.Read(_header, 0, _header.Length); // the normal end is here if (headerLength == 0) { _unpackedBuffer = null; return; } if (headerLength != _header.Length) { throw new InvalidDataException("QuickLZ input buffer corrupted (header)"); } //int sizeCompressed = _quickLZ.sizeCompressed(_header); int sizeCompressed = QuickLZ.sizeCompressed(_header); if (_readBuffer == null || _readBuffer.Length < sizeCompressed) { if (_readBuffer != null) { Free(_readBuffer); } _readBuffer = Alloc(sizeCompressed); } Buffer.BlockCopy(_header, 0, _readBuffer, 0, _header.Length); int bodyLength = _sourceStream.Read(_readBuffer, _header.Length, sizeCompressed - _header.Length); if (bodyLength != sizeCompressed - _header.Length) { throw new InvalidDataException("QuickLZ input buffer corrupted (body)"); } EnsureUnpackedBuffer(_readBuffer); //_unpackedLength = _quickLZ.decompress(_readBuffer, _unpackedBuffer); _unpackedBuffer = QuickLZ.decompress(_readBuffer); _unpackedLength = _unpackedBuffer.Length; _unpackedOffset = 0; }
/// <summary> /// Initializes a new instance of the <see cref="QuickLZDecompressionStream"/> class. /// </summary> /// <param name="sourceStream">The source stream.</param> public QuickLZDecompressionStream(Stream sourceStream) { _quickLZ = Pool.Default.Alloc(); _sourceStream = sourceStream; Fill(); }
/// <summary> /// Initializes a new instance of the <see cref="QuickLZDecompressionStream"/> class. /// </summary> /// <param name="sourceStream">The source stream.</param> public QuickLZDecompressionStream(Stream sourceStream) { _quickLZ = Pool.Default.Alloc(); _sourceStream = sourceStream; Fill(); }