internal void Dispose() { if (!_isDisposed && State != IntPtr.Zero) { BrotliNative.BrotliEncoderDestroyInstance(State); } _isDisposed = true; }
private void InitializeEncoder() { State = BrotliNative.BrotliEncoderCreateInstance(); if (State == IntPtr.Zero) { throw new Exception();//TODO Create exception } }
public void SetWindow(uint window) { if (window < MinWindowBits || window > MaxWindowBits) { throw new ArgumentException();//TODO } BrotliNative.BrotliEncoderSetParameter(State, BrotliNative.BrotliEncoderParameter.LGWin, window); }
public void SetQuality(uint quality) { if (quality < MinQuality || quality > MaxQuality) { throw new ArgumentException();//TODO } BrotliNative.BrotliEncoderSetParameter(State, BrotliNative.BrotliEncoderParameter.Quality, quality); }
private void InitializeDecoder() { State = BrotliNative.BrotliDecoderCreateInstance(); if (State == IntPtr.Zero) { throw new Exception();//TODO Create exception } BufferStream = new MemoryStream(); }
public override void Write(byte[] buffer, int offset, int count) { if (_mode != CompressionMode.Compress) { throw new Exception(); //WrongStreamMode Exeption } totalWrote += count; //Console.WriteLine(String.Format("Write {0} bytes,total={1} bytes.", count, totalWrote)); nuint totalOut = 0; int bytesRemain = count; int currentOffset = offset; int copyLen; while (bytesRemain > 0) { copyLen = bytesRemain > BufferSize ? BufferSize : bytesRemain; Marshal.Copy(buffer, currentOffset, BufferIn, copyLen); bytesRemain -= copyLen; currentOffset += copyLen; AvailIn = (IntPtr)copyLen; NextIn = BufferIn; while ((int)AvailIn > 0) { if (!BrotliNative.BrotliEncoderCompressStream(_encoder.State, BrotliNative.BrotliEncoderOperation.Process, ref AvailIn, ref NextIn, ref AvailOut, ref NextOut, out totalOut)) { throw new Exception(); //TODO } if ((nuint)AvailOut != BufferSize) { var bytesWrote = (int)(BufferSize - (nuint)AvailOut); Byte[] buf = new Byte[bytesWrote]; Marshal.Copy(BufferOut, buf, 0, bytesWrote); _stream.Write(buf, 0, bytesWrote); AvailOut = new IntPtr((uint)BufferSize); NextOut = BufferOut; } } if (BrotliNative.BrotliEncoderIsFinished(_encoder.State)) { break; } } }
protected virtual void FlushEncoder(Boolean finished) { if (_encoder.State == IntPtr.Zero) { return; } if (BrotliNative.BrotliEncoderIsFinished(_encoder.State)) { return; } BrotliNative.BrotliEncoderOperation op = finished ? BrotliNative.BrotliEncoderOperation.Finish : BrotliNative.BrotliEncoderOperation.Flush; UInt32 totalOut = 0; while (true) { var compressOK = BrotliNative.BrotliEncoderCompressStream(_encoder.State, op, ref AvailIn, ref NextIn, ref AvailOut, ref NextOut, out totalOut); if (!compressOK) { throw new Exception(); // unable encode } var extraData = (nuint)AvailOut != BufferSize; if (extraData) { var bytesWrote = (int)(BufferSize - (nuint)AvailOut); Byte[] buf = new Byte[bytesWrote]; Marshal.Copy(BufferOut, buf, 0, bytesWrote); _stream.Write(buf, 0, bytesWrote); AvailOut = (IntPtr)BufferSize; NextOut = BufferOut; } if (BrotliNative.BrotliEncoderIsFinished(_encoder.State)) { break; } if (!extraData) { break; } } }
public override int Read(byte[] buffer, int offset, int count) { if (_mode != CompressionMode.Decompress) { throw new Exception(); // wrong mode } int bytesRead = (int)(_decoder.BufferStream.Length - _readOffset); uint totalCount = 0; Boolean endOfStream = false; Boolean errorDetected = false; Byte[] buf = new Byte[BufferSize]; while (bytesRead < count) { while (true) { if (_decoder.LastDecoderResult == BrotliNative.BrotliDecoderResult.NeedsMoreInput) { AvailIn = (IntPtr)_stream.Read(buf, 0, (int)BufferSize); NextIn = BufferIn; if ((int)AvailIn <= 0) { endOfStream = true; break; } Marshal.Copy(buf, 0, BufferIn, (int)AvailIn); } else if (_decoder.LastDecoderResult == BrotliNative.BrotliDecoderResult.NeedsMoreOutput) { Marshal.Copy(BufferOut, buf, 0, BufferSize); _decoder.BufferStream.Write(buf, 0, BufferSize); bytesRead += BufferSize; AvailOut = new IntPtr((uint)BufferSize); NextOut = BufferOut; } else { //Error or OK endOfStream = true; break; } _decoder.LastDecoderResult = BrotliNative.BrotliDecoderDecompressStream(_decoder.State, ref AvailIn, ref NextIn, ref AvailOut, ref NextOut, out totalCount); if (bytesRead >= count) { break; } } if (endOfStream && !BrotliNative.BrotliDecoderIsFinished(_decoder.State)) { errorDetected = true; } if (_decoder.LastDecoderResult == BrotliNative.BrotliDecoderResult.Error || errorDetected) { var error = BrotliNative.BrotliDecoderGetErrorCode(_decoder.State); var text = BrotliNative.BrotliDecoderErrorString(error); throw new Exception(); //error - unable decode stream } if (endOfStream && !BrotliNative.BrotliDecoderIsFinished(_decoder.State) && _decoder.LastDecoderResult == BrotliNative.BrotliDecoderResult.NeedsMoreInput) { throw new Exception();//wrong end } if (endOfStream && NextOut != BufferOut) { int remainBytes = (int)(NextOut.ToInt64() - BufferOut.ToInt64()); bytesRead += remainBytes; Marshal.Copy(BufferOut, buf, 0, remainBytes); _decoder.BufferStream.Write(buf, 0, remainBytes); NextOut = BufferOut; } if (endOfStream) { break; } } if (_decoder.BufferStream.Length - _readOffset >= count || endOfStream) { _decoder.BufferStream.Seek(_readOffset, SeekOrigin.Begin); var bytesToRead = (int)(_decoder.BufferStream.Length - _readOffset); if (bytesToRead > count) { bytesToRead = count; } _decoder.BufferStream.Read(buffer, offset, bytesToRead); _decoder.RemoveBytes(_readOffset + bytesToRead); _readOffset = 0; return(bytesToRead); } return(0); }