Example #1
0
        public override int Read(Span <byte> buffer)
        {
            EnsureNotDisposed();

            var output = new ZSTD_outBuffer_s {
                pos = 0, size = (nuint)buffer.Length
            };

            while (output.pos < output.size)
            {
                if (input.pos >= input.size)
                {
                    int bytesRead;
                    if ((bytesRead = innerStream.Read(inputBuffer, 0, inputBufferSize)) == 0)
                    {
                        break;
                    }

                    input.size = (nuint)bytesRead;
                    input.pos  = 0;
                }

                lastDecompressResult = DecompressStream(ref output, buffer);
            }

            return((int)output.pos);
        }
Example #2
0
        public override async ValueTask <int> ReadAsync(Memory <byte> buffer,
                                                        CancellationToken cancellationToken = default)
        {
            EnsureNotDisposed();

            var output = new ZSTD_outBuffer_s {
                pos = 0, size = (nuint)buffer.Length
            };

            while (output.pos < output.size)
            {
                if (input.pos >= input.size)
                {
                    int bytesRead;
                    if ((bytesRead = await innerStream.ReadAsync(inputBuffer, 0, inputBufferSize, cancellationToken)
                                     .ConfigureAwait(false)) == 0)
                    {
                        break;
                    }

                    input.size = (nuint)bytesRead;
                    input.pos  = 0;
                }

                lastDecompressResult = DecompressStream(ref output, buffer.Span);
            }

            return((int)output.pos);
        }
Example #3
0
        public CompressionStream(Stream stream, int level = Compressor.DefaultCompressionLevel,
                                 int bufferSize           = 0)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanWrite)
            {
                throw new ArgumentException("Stream is not writable", nameof(stream));
            }

            if (bufferSize < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            innerStream = stream;
            compressor  = new Compressor(level);

            var outputBufferSize =
                bufferSize > 0 ? bufferSize : (int)Methods.ZSTD_CStreamOutSize().EnsureZstdSuccess();

            outputBuffer = ArrayPool <byte> .Shared.Rent(outputBufferSize);

            output = new ZSTD_outBuffer_s {
                pos = 0, size = (nuint)outputBufferSize
            };
        }
Example #4
0
 internal nuint DecompressStream(ref ZSTD_inBuffer_s input, ref ZSTD_outBuffer_s output)
 {
     fixed(ZSTD_inBuffer_s *inputPtr = &input)
     fixed(ZSTD_outBuffer_s * outputPtr = &output)
     {
         return(Methods.ZSTD_decompressStream(dctx, outputPtr, inputPtr).EnsureZstdSuccess());
     }
 }
Example #5
0
 private unsafe nuint DecompressStream(ref ZSTD_outBuffer_s output, Span <byte> outputBuffer)
 {
     fixed(byte *inputBufferPtr = inputBuffer)
     fixed(byte *outputBufferPtr = outputBuffer)
     {
         input.src  = inputBufferPtr;
         output.dst = outputBufferPtr;
         return(decompressor.DecompressStream(ref input, ref output));
     }
 }