Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SnappyStream"/> class.
        /// </summary>
        /// <param name="s">The stream.</param>
        /// <param name="mode">The compression mode.</param>
        /// <param name="leaveOpen">If set to <c>true</c> leaves the stream open when complete.</param>
        /// <param name="checksum"><c>true</c> if checksums should be written to the stream </param>
        public SnappyStream(Stream s, CompressionMode mode, bool leaveOpen, bool checksum)
        {
            stream          = s;
            compressionMode = mode;
            leaveStreamOpen = leaveOpen;
            writeChecksums  = checksum;

            if (compressionMode == CompressionMode.Decompress)
            {
                if (!stream.CanRead)
                {
                    throw new InvalidOperationException("Trying to decompress and underlying stream not readable.");
                }

                decompressor = new SnappyDecompressor();

                CheckStreamIdentifier();
                CheckStreamHeader();
            }
            if (compressionMode == CompressionMode.Compress)
            {
                if (!stream.CanWrite)
                {
                    throw new InvalidOperationException("Trying to compress and underlying stream is not writable.");
                }

                compressor = new SnappyCompressor();

                stream.WriteByte(StreamIdentifier);
                stream.Write(StreamHeader, 0, StreamHeader.Length);
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SnappyStream"/> class.
        /// </summary>
        /// <param name="s">The stream.</param>
        /// <param name="mode">The compression mode.</param>
        /// <param name="leaveOpen">If set to <c>true</c> leaves the stream open when complete.</param>
        /// <param name="checksum"><c>true</c> if checksums should be written to the stream </param>
        public SnappyStream(Stream s, CompressionMode mode, bool leaveOpen, bool checksum)
        {
            stream = s;
            compressionMode = mode;
            leaveStreamOpen = leaveOpen;
            writeChecksums = checksum;

            if (compressionMode == CompressionMode.Decompress)
            {
                if (!stream.CanRead)
                    throw new InvalidOperationException("Trying to decompress and underlying stream not readable.");

                decompressor =  new SnappyDecompressor();

                CheckStreamIdentifier();
                CheckStreamHeader();
            }
            if (compressionMode == CompressionMode.Compress)
            {
                if (!stream.CanWrite)
                    throw new InvalidOperationException("Trying to compress and underlying stream is not writable.");

                compressor = new SnappyCompressor();

                stream.WriteByte(StreamIdentifier);
                stream.Write(StreamHeader, 0, StreamHeader.Length);
            }
        }
Пример #3
0
 public static byte[] Compress(byte[] uncompressed)
 {
     var target = new SnappyCompressor();
     var result = new byte[target.MaxCompressedLength(uncompressed.Length)];
     var count = target.Compress(uncompressed, 0, uncompressed.Length, result);
     return result.Take(count).ToArray();
 }
Пример #4
0
        public static byte[] Compress(byte[] uncompressed)
        {
            var target = new SnappyCompressor();
            var result = new byte[target.MaxCompressedLength(uncompressed.Length)];
            var count  = target.Compress(uncompressed, 0, uncompressed.Length, result);

            return(result.Take(count).ToArray());
        }
Пример #5
0
        public static int MaxCompressedLength(int sourceLength)
        {
            var compressor = new SnappyCompressor();

            return(compressor.MaxCompressedLength(sourceLength));
        }
Пример #6
0
 public static int MaxCompressedLength(int sourceLength)
 {
     var compressor = new SnappyCompressor();
     return compressor.MaxCompressedLength(sourceLength);
 }