static void CompressText(string text, string destinationFile, Lz4CompressionLevel level)
 {
   using (var writer = new FileStream(destinationFile, FileMode.Create))
   using (var lz4Stream = new LZ4Stream(writer, CompressionMode.Compress, Lz4CompressionLevel.Fastest))
   {
     var lineBytes = Encoding.ASCII.GetBytes(text);
     lz4Stream.Write(lineBytes, 0, lineBytes.Length);
   }
 }
 static void CompressText(string text, string destinationFile, Lz4CompressionLevel level)
 {
     using (var writer = new FileStream(destinationFile, FileMode.Create))
         using (var lz4Stream = new LZ4Stream(writer, CompressionMode.Compress, Lz4CompressionLevel.Fastest))
         {
             var lineBytes = Encoding.ASCII.GetBytes(text);
             lz4Stream.Write(lineBytes, 0, lineBytes.Length);
         }
 }
Beispiel #3
0
        public void Lz4_Lz4CompressionLevel()
        {
            // Constructors
            var level = new Lz4CompressionLevel(5);

            Assert.AreEqual(5, level);

            level = new Lz4CompressionLevel(CompressionLevel.Fastest);
            Assert.AreEqual(1, level);

            level = new Lz4CompressionLevel(CompressionLevel.Optimal);
            Assert.AreEqual(9, level);

            try { level = new Lz4CompressionLevel(CompressionLevel.NoCompression); Assert.Fail("Constructor should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

            try { level = new Lz4CompressionLevel(-1); Assert.Fail("Constructor should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

            try { level = new Lz4CompressionLevel(17); Assert.Fail("Constructor should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

            try { level = new Lz4CompressionLevel((CompressionLevel)99); Assert.Fail("Constructor should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

            // Implicit conversion
            level = 8;
            Assert.AreEqual(8, level);

            // Equality
            Assert.IsTrue(new Lz4CompressionLevel(8) == level);
            Assert.IsFalse(new Lz4CompressionLevel(1) == level);
            Assert.IsTrue(new Lz4CompressionLevel(3) != level);
            Assert.IsFalse(new Lz4CompressionLevel(8) != level);

            object o = new Lz4CompressionLevel(2);

            Assert.IsTrue(o.Equals(new Lz4CompressionLevel(2)));
            Assert.IsFalse(o.Equals(new Lz4CompressionLevel(9)));
            Assert.IsTrue(new Lz4CompressionLevel(2).Equals(new Lz4CompressionLevel(2)));
            Assert.IsFalse(new Lz4CompressionLevel(8).Equals(new Lz4CompressionLevel(2)));
            Assert.IsFalse(o.Equals(null));
            Assert.IsFalse(o.Equals(new GzipMemoryUsageLevel(3)));

            // HashCode
            int hash = new Lz4CompressionLevel(CompressionLevel.Fastest).GetHashCode();

            Assert.AreNotEqual(0, hash);

            // ToString
            string s = new Lz4CompressionLevel(9).ToString();

            Assert.IsFalse(String.IsNullOrEmpty(s));
        }
 static void CompressFile(string sourceFile, string destinationFile, Lz4CompressionLevel level)
 {
     using (var writer = new FileStream(destinationFile, FileMode.Create))
         using (var sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
             using (var reader = new StreamReader(sourceStream))
                 using (var lz4Stream = new LZ4Stream(writer, CompressionMode.Compress, Lz4CompressionLevel.Fastest))
                 {
                     var allContent      = reader.ReadToEnd();
                     var allContentBytes = Encoding.ASCII.GetBytes(allContent);
                     lz4Stream.Write(allContentBytes, 0, allContentBytes.Length);
                 }
 }
 static void CompressFile(string sourceFile, string destinationFile, Lz4CompressionLevel level)
 {
   using (var writer = new FileStream(destinationFile, FileMode.Create))
   using (var sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
   using (var reader = new StreamReader(sourceStream))
   using (var lz4Stream = new LZ4Stream(writer, CompressionMode.Compress, Lz4CompressionLevel.Fastest))
   {
     var allContent = reader.ReadToEnd();
     var allContentBytes = Encoding.ASCII.GetBytes(allContent);
     lz4Stream.Write(allContentBytes, 0, allContentBytes.Length);
   }
 }
 private static void CompressWithLZ4EXE(string uncompressed, string compressed, Lz4CompressionLevel level)
 {
   var psi = new ProcessStartInfo {
     FileName = Lz4ExePath,
     CreateNoWindow = true,
     Arguments = $"-f -{(int) level} {uncompressed} {compressed}",
     UseShellExecute = false,
   };
   var p = Process.Start(psi);
   p.WaitForExit();
   if (p.ExitCode != 0)
     throw new Exception($"lz4 executable failed with error {p.ExitCode}");
 }
Beispiel #7
0
        public void DecompressionWithFixedBufferSize(
            [Values(4 * KB, 400 * KB, 4 * MB, 16 * MB)] int size,
            [Values(4 * KB, 400 * KB, 4 * MB, 16 * MB)] int bufferSize,
            [Values(Lz4CompressionLevel.Fastest, Lz4CompressionLevel.Best)] Lz4CompressionLevel level
            )
        {
            var uncompressedFile = PrepareFileWithRandomGarbage(size);
            var compressedFile   = AddExt(uncompressedFile, ".lz4");
            var testManagedFile  = AddExt(uncompressedFile, ".testmanaged");

            CompressWithLZ4EXE(uncompressedFile, compressedFile, level);
            UncompressWithLZ4Stream(compressedFile, testManagedFile, () => bufferSize, bufferSize);
            FileCompare(uncompressedFile, testManagedFile);
        }
Beispiel #8
0
        public void DecompressionWithRandomBufferSize(
            [Values(4 * KB, 400 * KB, 4 * MB, 8 * MB)] int size,
            [Values(111, 222, 333, 444, 555, 666, 777, 888, 999)] int bufferSizeSeed,
            [Values(Lz4CompressionLevel.Fastest, Lz4CompressionLevel.Best)] Lz4CompressionLevel level
            )
        {
            var uncompressedFile = PrepareFileWithRandomGarbage(size);
            var compressedFile   = AddExt(uncompressedFile, ".lz4");
            var testManagedFile  = AddExt(uncompressedFile, ".testmanaged");
            var r = new Random(bufferSizeSeed);

            CompressWithLZ4EXE(uncompressedFile, compressedFile, level);
            UncompressWithLZ4Stream(compressedFile, testManagedFile, () => r.Next(1, size), size);
            FileCompare(uncompressedFile, testManagedFile);
        }
Beispiel #9
0
        private static void CompressWithLZ4EXE(string uncompressed, string compressed, Lz4CompressionLevel level)
        {
            var psi = new ProcessStartInfo {
                FileName        = Lz4ExePath,
                CreateNoWindow  = true,
                Arguments       = $"-f -{(int) level} {uncompressed} {compressed}",
                UseShellExecute = false,
            };
            var p = Process.Start(psi);

            p.WaitForExit();
            if (p.ExitCode != 0)
            {
                throw new Exception($"lz4 executable failed with error {p.ExitCode}");
            }
        }
Beispiel #10
0
 private static void CompressWithLZ4Stream(string uncompressedFile, string compressedFile, Func <int> bufferSizeGenerator, int bufferSize, Lz4CompressionLevel level)
 {
     using (var us = File.OpenRead(uncompressedFile))
         using (var cs = File.OpenWrite(compressedFile))
             using (var lz4s = new LZ4Stream(cs, CompressionMode.Compress, level))
             {
                 us.CopyToWithBufferSizeGenerator(lz4s, bufferSizeGenerator, bufferSize);
             }
 }
    //readonly unsafe LZ4Preferences* _prefs;
    #endregion Private Members

    #region Ctor
    /// <summary>
    /// Initializes a new instance of the <see cref="LZ4Stream"/> class.
    /// </summary>
    /// <param name="baseStream">The base stream.</param>
    /// <param name="mode">The compression mode</param>
    /// <param name="level">The compression level. Relevant for <see cref="CompressionMode.Compress"/></param>
    /// <param name="blockSize">Size of the block.</param>
    /// <exception cref="ArgumentNullException"></exception>
    /// <exception cref="LZ4Exception">
    /// </exception>
    /// <exception cref="ArgumentOutOfRangeException">mode</exception>
    /// <exception cref="OutOfMemoryException">There is insufficient memory to satisfy the request.</exception>
    /// <exception cref="ArgumentException">Steam compression mode contradicts base-stream read/write availability</exception>
    public unsafe LZ4Stream(Stream baseStream, CompressionMode mode, Lz4CompressionLevel level = Lz4CompressionLevel.Fastest, LZ4BlockSize blockSize = LZ4BlockSize.Max4Mb)
    {
      if (baseStream == null) throw new ArgumentNullException(nameof(baseStream));
      if (mode == CompressionMode.Compress && !baseStream.CanWrite)
        throw new ArgumentException("stream is set to compress, but base-stream cannot be written to", nameof(baseStream));
      if (mode == CompressionMode.Decompress && !baseStream.CanRead)
        throw new ArgumentException("stream is set to decompress, but base-stream cannot be read from", nameof(baseStream));
      Contract.EndContractBlock();

      _baseStream = baseStream;
      _mode = mode;

      void* ctx;
      size_t rc;
      switch (mode) {
        case CompressionMode.Compress:
          rc = LZ4F_createCompressionContext(&ctx, Lz4Version);
          _ctx = ctx;

          if (LZ4F_isError(rc))
            throw new LZ4Exception(rc);

          LZ4Preferences prefs;
          prefs.AutoFlush = Lz4AutoFlush.Enabled;
          prefs.CompressionLevel = level;
          prefs.FrameInfo.BlockMode = LZ4BlockMode.Independent;
          prefs.FrameInfo.BlockSize = blockSize;
          prefs.FrameInfo.ContentChecksum = LZ4ContentChecksum.Enabled;

          MaxInputBufferSize = GetBlockSize(blockSize);
          _dstBuffer = new NativeBuffer(LZ4F_compressFrameBound((IntPtr) MaxInputBufferSize).ToInt32());

          var headerSize = LZ4F_compressBegin(_ctx, _dstBuffer.Ptr, (IntPtr) _dstBuffer.Size, &prefs);
          if (LZ4F_isError(headerSize))
            throw new LZ4Exception(headerSize,
              $"File header generation failed: {GetErrorName(headerSize)}");

          _baseStream.Write(_dstBuffer.Buffer, 0, headerSize.ToInt32());
          break;
        case CompressionMode.Decompress:
          rc = LZ4F_createDecompressionContext(&ctx, Lz4Version);
          _ctx = ctx;

          if (LZ4F_isError(rc))
            throw new LZ4Exception(rc);

          _srcBuffer = new NativeBuffer(DECOMPRESSION_BUFFER_SIZE);

          baseStream.Read(_srcBuffer.Buffer, 0, sizeof (int));

          var outSize = 0UL;
          var inSize = (ulong) sizeof (int);
          var frameHeader = stackalloc byte[MAX_FRAME_HEADER_SIZE];
          rc = LZ4F_decompress(_ctx, frameHeader, (IntPtr*) &outSize, _srcBuffer.Ptr, (IntPtr*) &inSize);

          if (LZ4F_isError(rc))
            throw new LZ4Exception(rc);

          _nextSizeToRead = (ulong) rc;
          break;
        default:
          throw new ArgumentOutOfRangeException(nameof(mode), $"Unsupported mode: {mode}");
      }
    }
 private static void CompressWithLZ4Stream(string uncompressedFile, string compressedFile, Func<int> bufferSizeGenerator, int bufferSize, Lz4CompressionLevel level)
 {
   using (var us = File.OpenRead(uncompressedFile))
   using (var cs = File.OpenWrite(compressedFile))
   using (var lz4s = new LZ4Stream(cs, CompressionMode.Compress, level))
   {
     us.CopyToWithBufferSizeGenerator(lz4s, bufferSizeGenerator, bufferSize);
   }
 }
Beispiel #13
0
        //readonly unsafe LZ4Preferences* _prefs;
        #endregion Private Members

        #region Ctor
        /// <summary>
        /// Initializes a new instance of the <see cref="LZ4Stream"/> class.
        /// </summary>
        /// <param name="baseStream">The base stream.</param>
        /// <param name="mode">The compression mode</param>
        /// <param name="level">The compression level. Relevant for <see cref="CompressionMode.Compress"/></param>
        /// <param name="blockSize">Size of the block.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="LZ4Exception">
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">mode</exception>
        /// <exception cref="OutOfMemoryException">There is insufficient memory to satisfy the request.</exception>
        /// <exception cref="ArgumentException">Steam compression mode contradicts base-stream read/write availability</exception>
        public unsafe LZ4Stream(Stream baseStream, CompressionMode mode, Lz4CompressionLevel level = Lz4CompressionLevel.Fastest, LZ4BlockSize blockSize = LZ4BlockSize.Max4Mb)
        {
            if (baseStream == null)
            {
                throw new ArgumentNullException(nameof(baseStream));
            }
            if (mode == CompressionMode.Compress && !baseStream.CanWrite)
            {
                throw new ArgumentException("stream is set to compress, but base-stream cannot be written to", nameof(baseStream));
            }
            if (mode == CompressionMode.Decompress && !baseStream.CanRead)
            {
                throw new ArgumentException("stream is set to decompress, but base-stream cannot be read from", nameof(baseStream));
            }
            Contract.EndContractBlock();

            _baseStream = baseStream;
            _mode       = mode;

            void * ctx;
            size_t rc;

            switch (mode)
            {
            case CompressionMode.Compress:
                rc   = LZ4F_createCompressionContext(&ctx, Lz4Version);
                _ctx = ctx;

                if (LZ4F_isError(rc))
                {
                    throw new LZ4Exception(rc);
                }

                LZ4Preferences prefs;
                prefs.AutoFlush                 = Lz4AutoFlush.Enabled;
                prefs.CompressionLevel          = level;
                prefs.FrameInfo.BlockMode       = LZ4BlockMode.Independent;
                prefs.FrameInfo.BlockSize       = blockSize;
                prefs.FrameInfo.ContentChecksum = LZ4ContentChecksum.Enabled;

                MaxInputBufferSize = GetBlockSize(blockSize);
                _dstBuffer         = new NativeBuffer(LZ4F_compressFrameBound((IntPtr)MaxInputBufferSize).ToInt32());

                var headerSize = LZ4F_compressBegin(_ctx, _dstBuffer.Ptr, (IntPtr)_dstBuffer.Size, &prefs);
                if (LZ4F_isError(headerSize))
                {
                    throw new LZ4Exception(headerSize,
                                           $"File header generation failed: {GetErrorName(headerSize)}");
                }

                _baseStream.Write(_dstBuffer.Buffer, 0, headerSize.ToInt32());
                break;

            case CompressionMode.Decompress:
                rc   = LZ4F_createDecompressionContext(&ctx, Lz4Version);
                _ctx = ctx;

                if (LZ4F_isError(rc))
                {
                    throw new LZ4Exception(rc);
                }

                _srcBuffer = new NativeBuffer(DECOMPRESSION_BUFFER_SIZE);

                baseStream.Read(_srcBuffer.Buffer, 0, sizeof(int));

                var outSize     = 0UL;
                var inSize      = (ulong)sizeof(int);
                var frameHeader = stackalloc byte[MAX_FRAME_HEADER_SIZE];
                rc = LZ4F_decompress(_ctx, frameHeader, (IntPtr *)&outSize, _srcBuffer.Ptr, (IntPtr *)&inSize);

                if (LZ4F_isError(rc))
                {
                    throw new LZ4Exception(rc);
                }

                _nextSizeToRead = (ulong)rc;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), $"Unsupported mode: {mode}");
            }
        }