private static void SelectCodec(out ILZ4Service encoder, out ILZ4Service decoder, out ILZ4Service encoderHC) { // refer to: http://lz4net.codeplex.com/wikipage?title=Performance%20Testing for explanation about this order // feel free to change preferred order, just don't do it willy-nilly back it up with some evidence // it has been tested for Intel on Microsoft .NET only but looks reasonable for Mono as well if (IntPtr.Size == 4) { encoder = _service_N32 ?? _service_N64; decoder = _service_N64 ?? _service_N32; encoderHC = _service_N32 ?? _service_N64; } else { encoder = _service_N64 ?? _service_N32; decoder = _service_N64 ?? _service_N32; encoderHC = _service_N32 ?? _service_N64; } }
// ReSharper restore InconsistentNaming #endregion #region initialization /// <summary>Initializes the <see cref="LZ4Codec" /> class.</summary> static LZ4Codec() { // NOTE: this method exploits the fact that assemblies are loaded first time they // are needed so we can safely try load and handle if not loaded // I may change in future versions of .NET if (Try(Has2015Runtime, false)) { Try(InitializeLZ4mm); Try(InitializeLZ4cc); } Try(InitializeLZ4n); Try(InitializeLZ4s); ILZ4Service encoder, decoder, encoderHC; SelectCodec(out encoder, out decoder, out encoderHC); Encoder = encoder; Decoder = decoder; EncoderHC = encoderHC; if (Encoder == null || Decoder == null) { throw new NotSupportedException("No LZ4 compression service found"); } }
// ReSharper restore InconsistentNaming #endregion #region initialization /// <summary>Initializes the <see cref="LZ4Codec" /> class.</summary> static LZ4Codec() { InitializeLZ4s(); ILZ4Service encoder, decoder, encoderHC; SelectCodec(out encoder, out decoder, out encoderHC); Encoder = encoder; Decoder = decoder; EncoderHC = encoderHC; if (Encoder == null || Decoder == null) { throw new NotSupportedException("No LZ4 compression service found"); } }
// ReSharper restore InconsistentNaming #endregion #region initialization /// <summary>Initializes the <see cref="LZ4Codec"/> class.</summary> static LZ4Codec() { // NOTE: this method exploits the fact that assemblies are loaded first time they // are needed so we can safely try load and handle if not loaded // I may change in future versions of .NET Try(InitializeLZ4Native); // refer to: http://lz4net.codeplex.com/wikipage?title=Performance%20Testing // for explanation about this order // feel free to change preferred order, just don't do it willy-nilly // back it up with some evidence if (IntPtr.Size == 4) { encoder = _service_Native32 ?? _service_Native64; decoder = _service_Native32 ?? _service_Native64; encoderHC = _service_Native32 ?? _service_Native64; } else { encoder = _service_Native64 ?? _service_Native32; decoder = _service_Native64 ?? _service_Native32; encoderHC = _service_Native64 ?? _service_Native32; } if (encoder == null || decoder == null) { throw new NotSupportedException("No LZ4 compression service found"); } }
// ReSharper restore InconsistentNaming #endregion #region initialization /// <summary>Initializes the <see cref="LZ4Codec" /> class.</summary> static LZ4Codec() { // NOTE: this method exploits the fact that assemblies are loaded first time they // are needed so we can safely try load and handle if not loaded // I may change in future versions of .NET _service_S32 = TryService <Services.Safe32LZ4Service>(); _service_S64 = TryService <Services.Safe64LZ4Service>(); ILZ4Service encoder, decoder, encoderHC; SelectCodec(out encoder, out decoder, out encoderHC); Encoder = encoder; Decoder = decoder; EncoderHC = encoderHC; if (Encoder == null || Decoder == null) { throw new NotSupportedException("No LZ4 compression service found"); } }
// ReSharper restore InconsistentNaming #endregion #region initialization /// <summary>Initializes the <see cref="LZ4Codec" /> class.</summary> static LZ4Codec() { Try(InitializeLZ4s); // refer to: http://lz4net.codeplex.com/wikipage?title=Performance%20Testing // for explanation about this order // feel free to change preferred order, just don't do it willy-nilly // back it up with some evidence if (IntPtr.Size == 4) { Encoder = _service_S32 ?? _service_S64; Decoder = _service_S64 ?? _service_S32; EncoderHC = _service_S32 ?? _service_S64; } else { Encoder = _service_S32 ?? _service_S64; Decoder = _service_S64 ?? _service_S32; EncoderHC = _service_S32 ?? _service_S64; } if (Encoder == null || Decoder == null) { throw new NotSupportedException("No LZ4 compression service found"); } }
/// <summary>Performs the quick auto-test on given compression service.</summary> /// <param name="service">The service.</param> /// <returns>A service or <c>null</c> if it failed.</returns> private static ILZ4Service AutoTest(ILZ4Service service) { const string loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut " + "labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " + "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in " + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " + "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; // generate some well-known array of bytes const string inputText = loremIpsum + loremIpsum + loremIpsum + loremIpsum + loremIpsum; var original = Encoding.UTF8.GetBytes(inputText); // LZ4 test { // compress it var encoded = new byte[MaximumOutputLength(original.Length)]; var encodedLength = service.Encode(original, 0, original.Length, encoded, 0, encoded.Length); if (encodedLength < 0) { return(null); } // decompress it (knowing original length) var decoded = new byte[original.Length]; var decodedLength1 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, true); if (decodedLength1 != original.Length) { return(null); } var outputText1 = Encoding.UTF8.GetString(decoded, 0, decoded.Length); if (outputText1 != inputText) { return(null); } // decompress it (not knowing original length) var decodedLength2 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, false); if (decodedLength2 != original.Length) { return(null); } var outputText2 = Encoding.UTF8.GetString(decoded, 0, decoded.Length); if (outputText2 != inputText) { return(null); } } // LZ4HC { // compress it var encoded = new byte[MaximumOutputLength(original.Length)]; var encodedLength = service.EncodeHC(original, 0, original.Length, encoded, 0, encoded.Length); if (encodedLength < 0) { return(null); } // decompress it (knowing original length) var decoded = new byte[original.Length]; var decodedLength1 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, true); if (decodedLength1 != original.Length) { return(null); } var outputText1 = Encoding.UTF8.GetString(decoded, 0, decoded.Length); if (outputText1 != inputText) { return(null); } // decompress it (not knowing original length) var decodedLength2 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, false); if (decodedLength2 != original.Length) { return(null); } var outputText2 = Encoding.UTF8.GetString(decoded, 0, decoded.Length); if (outputText2 != inputText) { return(null); } } return(service); }
private static void InitializeLZ4Native() { _service_Native = Try <NativeLz4Service>(); }
private static ILZ4Service NoAutoTest(ILZ4Service service) { return(service); }
private static void InitializeLZ4Native() { _service_Native32 = Try <Native32LZ4Service>(); _service_Native64 = Try <Native64LZ4Service>(); }
private static void InitializeLZ4s() { _service_S32 = Try <Safe32LZ4Service>(); _service_S64 = Try <Safe64LZ4Service>(); }