示例#1
0
        /// <summary>
        /// Applies a compressin to the buffer and returns a result within a stream.
        /// </summary>
        /// <param name="buffer">The buffer to compress.</param>
        /// <param name="padding">The padding offset to pre-allocate.</param>
        /// <param name="context">The context of the processing.</param>
        /// <returns>The length segment</returns>
        public static BufferSegment Process(ByteStream stream, int padding, ProcessingContext context)
        {
            stream.Flush();

            // Calculate the maximum compressed length
            var outSize = Snappy.MaxEncodedLen((int)stream.Length);
            var output  = context.BufferReserve(outSize + padding);

            // Acquire a snappy encoder which comes in with a table for state
            using (var snappy = Snappy.Acquire())
            {
                output.Size = snappy.Encode(stream.GetBuffer(), 0, (int)stream.Length, output, padding) + padding;
                return(output);
            }
        }