public BrotliStream(Stream baseStream, CompressionMode mode, bool leaveOpen) { if (baseStream == null) { throw new ArgumentNullException("baseStream"); } _mode = mode; _stream = baseStream; _leaveOpen = leaveOpen; if (_mode == CompressionMode.Compress) { _state = Brolib.BrotliEncoderCreateInstance(); if (_state == IntPtr.Zero) { throw new BrotliException("Unable to create brotli encoder instance"); } Brolib.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.Quality, 5); Brolib.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.LGWin, 22); } else { _state = Brolib.BrotliDecoderCreateInstance(); if (_state == IntPtr.Zero) { throw new BrotliException("Unable to create brotli decoder instance"); } } _ptrInputBuffer = Marshal.AllocHGlobal(BufferSize); _ptrOutputBuffer = Marshal.AllocHGlobal(BufferSize); _ptrNextInput = _ptrInputBuffer; _ptrNextOutput = _ptrOutputBuffer; _managedBuffer = new Byte[BufferSize]; }
/// <summary> /// Set the compress LGWin(10~24) /// </summary> /// <param name="window">the window size</param> public void SetWindow(uint window) { if (window < 10 || window > 24) { throw new ArgumentException("window", "the range of window is 10~24"); } Brolib.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.LGWin, window); }
/// <summary> /// Set the compress quality(0~11) /// </summary> /// <param name="quality">compress quality</param> public void SetQuality(uint quality) { if (quality < 0 || quality > 11) { throw new ArgumentException("quality", "the range of quality is 0~11"); } Brolib.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.Quality, quality); }
static IntPtr CreateEncoderInstance(uint quality, uint windowSize) { IntPtr result = Brolib.BrotliEncoderCreateInstance(); if (result == IntPtr.Zero) { throw new BrotliException("Unable to create brotli encoder instance"); } Brolib.BrotliEncoderSetParameter( result, BrotliEncoderParameter.Quality, quality); Brolib.BrotliEncoderSetParameter( result, BrotliEncoderParameter.LGWin, windowSize); return(result); }