internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, bool usingGZip){                                                                                          
            _stream = stream;
            _mode = mode;
            _leaveOpen = leaveOpen;

            if(_stream == null ) {
                throw new ArgumentNullException("stream");
            }

            switch (_mode) {
            case CompressionMode.Decompress:
                if (!(_stream.CanRead)) {
                    throw new ArgumentException(SR.GetString(SR.NotReadableStream), "stream");
                }
                inflater = new Inflater(usingGZip);
                m_CallBack = new AsyncCallback(ReadCallback); 
                break;

            case CompressionMode.Compress:
                if (!(_stream.CanWrite)) {
                    throw new ArgumentException(SR.GetString(SR.NotWriteableStream), "stream");
                }
                deflater = new Deflater(usingGZip);
                m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                m_CallBack = new AsyncCallback(WriteCallback); 
                break;    

            default:
                throw new ArgumentException(SR.GetString(SR.ArgumentOutOfRange_Enum), "mode");
            }           
            buffer = new byte[bufferSize];
        }
Exemple #2
0
        public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            
            switch (mode)
            {
                case CompressionMode.Decompress:
                    if (!stream.CanRead)
                    {
                        throw new ArgumentException(SR.NotReadableStream, "stream");
                    }
                    _inflater = CreateInflater();
                    break;

                case CompressionMode.Compress:
                    if (!stream.CanWrite)
                    {
                        throw new ArgumentException(SR.NotWriteableStream, "stream");
                    }
                    _deflater = CreateDeflater(null);
                    break;

                default:
                    throw new ArgumentException(SR.ArgumentOutOfRange_Enum, "mode");
            }

            _stream = stream;
            _mode = mode;
            _leaveOpen = leaveOpen;
            _buffer = new byte[DefaultBufferSize];
        }
        public TextureNative(SectionHeader header, Stream stream)
            : base(header, stream)
        {
            SectionHeader.Read(stream);
            var reader = new BinaryReader(stream);

            PlatformID = reader.ReadUInt32();
            FilterFlags = (Filter) reader.ReadUInt16();
            WrapV = (WrapMode) reader.ReadByte();
            WrapU = (WrapMode) reader.ReadByte();
            DiffuseName = reader.ReadString(32);
            AlphaName = reader.ReadString(32);
            Format = (RasterFormat) reader.ReadUInt32();

            if (PlatformID == 9) {
                var dxt = reader.ReadString(4);
                switch (dxt) {
                    case "DXT1":
                        Compression = CompressionMode.DXT1;
                        break;
                    case "DXT3":
                        Compression = CompressionMode.DXT3; break;
                    default:
                        Compression = CompressionMode.None; break;
                }
            } else {
                Alpha = reader.ReadUInt32() == 0x1;
            }

            Width = reader.ReadUInt16();
            Height = reader.ReadUInt16();
            BPP = (byte) (reader.ReadByte() >> 3);
            MipMapCount = reader.ReadByte();
            RasterType = reader.ReadByte();

            if (RasterType != 0x4) {
                throw new Exception("Unexpected RasterType, expected 0x04.");
            }

            if (PlatformID == 9) {
                Alpha = (reader.ReadByte() & 0x1) == 0x1;
            } else {
                Compression = (CompressionMode) reader.ReadByte();
            }

            ImageDataSize = reader.ReadInt32();

            ImageData = reader.ReadBytes(ImageDataSize);

            if ((Format & RasterFormat.ExtMipMap) != 0) {
                var tot = ImageDataSize;
                for (var i = 0; i < MipMapCount; ++i) {
                    tot += ImageDataSize >> (2 * i);
                }

                ImageLevelData = reader.ReadBytes(tot);
            } else {
                ImageLevelData = ImageData;
            }
        }
 public ZlibBaseStream(System.IO.Stream stream,
                       CompressionMode compressionMode,
                       CompressionLevel level,
                       ZlibStreamFlavor flavor,
                       bool leaveOpen)
     :this(stream, compressionMode, level, flavor,leaveOpen, ZlibConstants.WindowBitsDefault)
 { }
 public ZOutputStream(Stream output, CompressionLevel level, bool nowrap)
     : this()
 {
     this._output = output;
     this._compressor = new Deflate(level, nowrap);
     this._compressionMode = CompressionMode.Compress;
 }
Exemple #6
0
        internal LZ4NetStream(Stream innerStream, CompressionMode compressionMode, bool leaveInnerStreamOpen,
                              LZ4Codec codec)
        {
            if (innerStream == null)
            {
                throw new ArgumentNullException(nameof(innerStream));
            }
            switch (compressionMode)
            {
            case CompressionMode.Compress:
            case CompressionMode.Decompress:
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(compressionMode));
            }
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            this.innerStream = innerStream;
            this.compressionMode = compressionMode;
            this.leaveInnerStreamOpen = leaveInnerStreamOpen;
            this.codec = codec;
        }
Exemple #7
0
 public void ZlibStreamConstructorTest()
 {
     Stream stream = null; // TODO: Initialize to an appropriate value
     CompressionMode mode = new CompressionMode(); // TODO: Initialize to an appropriate value
     ZlibStream target = new ZlibStream(stream, mode);
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
Exemple #8
0
		internal DeflateStream (Stream compressedStream, CompressionMode mode, bool leaveOpen, bool gzip) {
			if (compressedStream == null)
				throw new ArgumentNullException ("compressedStream");

			switch (mode) {
			case CompressionMode.Compress:
				if (!compressedStream.CanWrite)
					throw new ArgumentException ("The base stream is not writeable.");
				OutputStream outStream = new OutputStreamImpl(compressedStream);
				_writer = gzip ? new GZIPOutputStream (outStream) : new DeflaterOutputStream (outStream, new Deflater (Deflater.DEFAULT_COMPRESSION, true));
				break;
			case CompressionMode.Decompress:
				if (!compressedStream.CanRead)
					throw new ArgumentException ("The base stream is not readable.");
				InputStream inStream = new InputStreamImpl (compressedStream);
				_reader = gzip ? new GZIPInputStream (inStream) : new InflaterInputStream (inStream, new Inflater (true));
				break;
			default:
				throw new ArgumentException ("mode");
			}

			_baseStream = compressedStream;
			_leaveOpen = leaveOpen;
			_open = true;
		}
Exemple #9
0
 /// <summary>
 /// 根据压缩类型获取压缩数据流
 /// </summary>
 /// <param name="dataStream">原始数据流</param>
 /// <param name="mode">压缩模式</param>
 /// <param name="leaveOpen">是否流保留为打开状态</param>
 /// <param name="type">压缩类型</param>
 /// <returns>数据流</returns>
 private static Stream toCompressStream(this Stream dataStream, CompressionMode mode, bool leaveOpen, compression type)
 {
     Stream stream = dataStream;
     if (type == compression.GZip) stream = (Stream)new GZipStream(dataStream, mode, leaveOpen);
     else stream = (Stream)new DeflateStream(dataStream, mode, leaveOpen);
     return stream;
 }
Exemple #10
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);
            }
        }
        public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
        {
            this._stream = stream;
            this._mode = mode;
            this._leaveOpen = leaveOpen;
            if (this._stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            switch (this._mode)
            {
                case CompressionMode.Decompress:
                    if (!this._stream.CanRead)
                    {
                        throw new ArgumentException(SR.GetString("NotReadableStream"), "stream");
                    }
                    this.inflater = new Inflater();
                    this.m_CallBack = new AsyncCallback(this.ReadCallback);
                    break;

                case CompressionMode.Compress:
                    if (!this._stream.CanWrite)
                    {
                        throw new ArgumentException(SR.GetString("NotWriteableStream"), "stream");
                    }
                    this.deflater = new Deflater();
                    this.m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                    this.m_CallBack = new AsyncCallback(this.WriteCallback);
                    break;

                default:
                    throw new ArgumentException(SR.GetString("ArgumentOutOfRange_Enum"), "mode");
            }
            this.buffer = new byte[0x1000];
        }
Exemple #12
0
 public static void zlibDeflate(
     string pathInput,
     string pathOutput,
     CompressionMode mode,
     CompressionLevel level
 )
 {
     using (Stream input = File.OpenRead(pathInput))
     using (Stream output = File.Create(pathOutput))
     using (Stream deflateStream = new DeflateStream(
         mode == CompressionMode.Compress ? output : input,
         mode, level, true))
     {
         byte[] buff = new byte[ZLIB_BUFF_SIZE];
         int n = 0;
         Stream toRead = mode == CompressionMode.Compress ?
             input : deflateStream;
         Stream toWrite = mode == CompressionMode.Compress ?
             deflateStream : output;
         while (0 != (n = toRead.Read(buff, 0, buff.Length)))
         {
             toWrite.Write(buff, 0, n);
         }
         deflateStream.Close();
         input.Close();
         output.Close();
     }
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="contentEncoding"></param>
 /// <param name="stream"></param>
 /// <param name="mode"></param>
 public CompressedStream(string contentEncoding, Stream stream, CompressionMode mode)
 {
     if (contentEncoding.IndexOf(_contentIsGZipToken, StringComparison.InvariantCultureIgnoreCase) != -1)
         _gzipStream = new GZipStream(stream, mode);
     else if (contentEncoding != null && contentEncoding.IndexOf(_contentIsDeflateToken, StringComparison.InvariantCultureIgnoreCase) != -1)
         _deflateStream = new DeflateStream(stream, mode);
 }
 public ZOutputStream(Stream output)
     : this()
 {
     this._output = output;
     this._compressor = new Inflate();
     this._compressionMode = CompressionMode.Decompress;
 }
 public BufferedDeflateStream( int bufferSize, Stream stream, CompressionMode mode, bool leaveOpen )
     : base(stream, mode, leaveOpen)
 {
     buffer = new byte[ bufferSize ];
       bSize  = bufferSize;
       bPtr   = 0;
 }
        internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, bool usingGZip)
        {
            this._stream = stream;
            this._mode = mode;
            this._leaveOpen = leaveOpen;
            if (this._stream == null) {
                throw new ArgumentNullException("stream");
            }
            switch (this._mode) {
                case CompressionMode.Decompress:
                    if (!this._stream.CanRead) {
                        throw new ArgumentException("The base stream is not readable.", "stream");
                    }
                    this.inflater = new Inflater(usingGZip);
                    this.m_CallBack = new AsyncCallback(this.ReadCallback);
                    break;

                case CompressionMode.Compress:
                    if (!this._stream.CanWrite) {
                        throw new ArgumentException("The base stream is not writeable.", "stream");
                    }
                    this.deflater = new Deflater(usingGZip);
                    this.m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                    this.m_CallBack = new AsyncCallback(this.WriteCallback);
                    break;

                default:
                    throw new ArgumentException("Enum value was out of legal range.", "mode");
            }
            this.buffer = new byte[0x1000];
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ZlibStream"/> class by using the specified stream and compression mode, and optionally leaves the stream open.
        /// </summary>
        /// <param name="stream">The stream to compress or decompress.</param>
        /// <param name="mode">One of the enumeration values that indicates whether to compress or decompress the stream.</param>
        /// <param name="leaveOpen">true to leave the stream open after disposing the DeflateStream object; otherwise, false.</param>
        public ZlibStream(Stream stream, CompressionMode mode, bool leaveOpen)
            : base(stream, mode, true)
        {
            _stream = stream;
            _leaveOpen = leaveOpen;

            WriteHeader();
        }
Exemple #18
0
        /// <summary>Initializes a new instance of the <see cref="LZ4Stream" /> class.</summary>
        /// <param name="innerStream">The inner stream.</param>
        /// <param name="compressionMode">The compression mode.</param>
        /// <param name="highCompression">if set to <c>true</c> [high compression].</param>
        /// <param name="blockSize">Size of the block.</param>
        public LZ4Stream(
			Stream innerStream,
			CompressionMode compressionMode,
			bool highCompression = false,
			int blockSize = 1024*1024)
            : this(innerStream, ToLZ4StreamMode(compressionMode), highCompression, blockSize)
        {
        }
        /// <summary>
        /// This constructor will create a request that has has it's initial compression
        /// attributes set to the parameter values.
        /// </summary>
        /// <param name="responseCompression">
        /// Set to true to send a zipped request, false to send plain response.
        /// </param>
        /// <param name="requestCompression">
        /// Set to Automatic to use automatic acceptance of zipped/uncompressed response (accept zipped in most cases), Plain to accept plain responses only.
        /// </param>
        public SforceServiceWrapper(CompressionMode requestCompression, CompressionMode responseCompression)
        {
            RequestCompression = requestCompression;
            ResponseCompression = responseCompression;

            this.EnableDecompression = true;
            this.Timeout = soapTimeout;
        }
Exemple #20
0
 /// <summary>Create a <c>GZipStream</c> using the specified <c>CompressionMode</c> and the specified <c>CompressionLevel</c>, and explicitly specify whether the stream should be left open after Deflation or Inflation.</summary>
 /// <remarks><para>This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs.  By default, after<c>Close()</c> is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a memory stream that will be re-read after compressed data has been written to it.  Specify true for the <paramref name="leaveOpen" /> parameter to leave the stream open.</para><para>As noted in the class documentation, the <c>CompressionMode</c> (Compress or Decompress) also establishes the "direction" of the stream.  A<c>GZipStream</c> with <c>CompressionMode.Compress</c> works only through<c>Write()</c>.  A <c>GZipStream</c> with <c>CompressionMode.Decompress</c> works only through <c>Read()</c>.</para></remarks>
 /// <example>
 ///   This example shows how to use a <c>GZipStream</c> to compress data.
 ///   <code>using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) { using (var raw = System.IO.File.Create(outputFile)) { using (Stream compressor = new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, true)) { byte[] buffer = new byte[WORKING_BUFFER_SIZE]; int n; while ((n= input.Read(buffer, 0, buffer.Length)) != 0) { compressor.Write(buffer, 0, n); } } } }</code>
 ///   <code lang = "VB">
 ///     Dim outputFile As String = (fileToCompress &amp; ".compressed") Using input As Stream =
 ///     File.OpenRead(fileToCompress) Using raw As FileStream = File.Create(outputFile) Using compressor As
 ///     Stream = New GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, True) Dim
 ///     buffer As Byte() = New Byte(4096) {} Dim n As Integer = -1 Do While (n &lt;&gt; 0) If (n &gt; 0) Then
 ///     compressor.Write(buffer, 0, n) End If n = input.Read(buffer, 0, buffer.Length) Loop End Using End Using
 ///     End Using
 ///   </code>
 /// </example>
 /// <param name="stream">The stream which will be read or written.</param>
 /// <param name="mode">Indicates whether the GZipStream will compress or decompress.</param>
 /// <param name="level">A tuning knob to trade speed for effectiveness.</param>
 /// <param name="leaveOpen">true if the application would like the stream to remain open after inflation/deflation.</param>
 public GZipStream(
     Stream stream, 
     CompressionMode mode, 
     CompressionLevel level = CompressionLevel.Default, 
     bool leaveOpen = false)
 {
     this.baseStream = new ZlibBaseStream(stream, mode, level, ZlibStreamFlavor.Gzip, leaveOpen);
 }
 public static IInitStrategy BuildStrategy(CompressionMode mode)
 {
     switch (mode)
     {
         case CompressionMode.Compress: return new CompressInit();
         case CompressionMode.Decompress: return new DecompressInit();
         default: throw new ArgumentOutOfRangeException("mode");
     }
 }
Exemple #22
0
 /// <summary>Initializes a new instance of the <see cref="LZ4Stream" /> class.</summary>
 /// <param name="innerStream">The inner stream.</param>
 /// <param name="compressionMode">The compression mode.</param>
 /// <param name="blockSize">Size of the block.</param>
 public LZ4Stream(
     Stream innerStream,
     CompressionMode compressionMode,
     int blockSize = 1024*1024)
 {
     _innerStream = innerStream;
     _compressionMode = compressionMode;
     _blockSize = Math.Max(16, blockSize);
 }
 public static IOperation Build(CompressionMode mode, int blockLenght)
 {
     switch (mode)
     {
         case CompressionMode.Compress: return new Compress(blockLenght);
         case CompressionMode.Decompress: return new Decompress(blockLenght);
         default: throw new ArgumentOutOfRangeException("mode");
     }
 }
Exemple #24
0
        public GZipStream(Stream sBase, CompressionMode cm)
        {
            if(cm == CompressionMode.Compress)
                m_gzStream = new GZipOutputStream(sBase);
            else
                m_gzStream = new GZipInputStream(sBase);

            m_cprMode = cm;
        }
        public TextureNativeSectionData( SectionHeader header, FramedStream stream )
        {
            SectionHeader dataHeader = new SectionHeader( stream );
            BinaryReader reader = new BinaryReader( stream );

            PlatformID = reader.ReadUInt32();
            FilterFlags = (Filter) reader.ReadUInt16();
            WrapV = (WrapMode) reader.ReadByte();
            WrapU = (WrapMode) reader.ReadByte();
            DiffuseName = reader.ReadString( 32 );
            AlphaName = reader.ReadString( 32 );
            Format = (RasterFormat) reader.ReadUInt32();

            if ( PlatformID == 9 )
            {
                String dxt = reader.ReadString( 4 );
                switch ( dxt )
                {
                    case "DXT1":
                        Compression = CompressionMode.DXT1; break;
                    case "DXT3":
                        Compression = CompressionMode.DXT3; break;
                    default:
                        Compression = CompressionMode.None; break;
                }
            }
            else
                Alpha = reader.ReadUInt32() == 0x1;

            Width = reader.ReadUInt16();
            Height = reader.ReadUInt16();
            BPP = (byte) ( reader.ReadByte() >> 3 );
            MipMapCount = reader.ReadByte();
            RasterType = reader.ReadByte();

            if ( RasterType != 0x4 )
                throw new Exception( "Unexpected RasterType, expected 0x04." );

            if ( PlatformID == 9 )
                Alpha = ( reader.ReadByte() & 0x1 ) == 0x1;
            else
                Compression = (CompressionMode) reader.ReadByte();

            ImageDataSize = reader.ReadUInt32();
            if ( ( Format & RasterFormat.ExtMipMap ) != 0 )
            {
                ImageLevelData = new byte[ MipMapCount ][];
                for ( int i = 0; i < MipMapCount; ++i )
                    ImageLevelData[ i ] = reader.ReadBytes( (int) ImageDataSize >> ( 2 * i ) );
            }
            else
            {
                ImageLevelData = new byte[ 1 ][];
                ImageLevelData[ 0 ] = reader.ReadBytes( (int) ImageDataSize );
            }
        }
Exemple #26
0
        public ZlibStream(Stream stream, CompressionMode mode, bool leaveOpen)
            : base(stream, mode, true)
        {
            this.stream = stream;
            this.leaveOpen = leaveOpen;
            this.mode = mode;

            this.firstReadWrite = true;
            this.adler32 = new Adler32();
        }
 protected static Stream Compression(Stream stream, CompressionType type, CompressionMode mode)
 {
     switch (type)
       {
     case CompressionType.GZip:
       return new GZipStream(stream, mode);
     default:
       return new DeflateStream(stream, mode);
       }
 }
Exemple #28
0
 public void WriteTest()
 {
     Stream stream = null; // TODO: Initialize to an appropriate value
     CompressionMode mode = new CompressionMode(); // TODO: Initialize to an appropriate value
     ZlibStream target = new ZlibStream(stream, mode); // TODO: Initialize to an appropriate value
     byte[] buffer = null; // TODO: Initialize to an appropriate value
     int offset = 0; // TODO: Initialize to an appropriate value
     int count = 0; // TODO: Initialize to an appropriate value
     target.Write(buffer, offset, count);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
        /// <summary>Initializes a new instance of the <see cref="LZ4Stream" /> class.</summary>
        /// <param name="innerStream">The inner stream.</param>
        /// <param name="compressionMode">The compression mode.</param>
        /// <param name="compressionFlags">The compression flags.</param>
        /// <param name="blockSize">Size of the block.</param>
        public LZ4Stream(
			Stream innerStream,
			CompressionMode compressionMode,
			LZ4StreamFlags compressionFlags = LZ4StreamFlags.Default,
			int blockSize = 1024*1024)
            : this(innerStream,
				ToLZ4StreamMode(compressionMode),
				compressionFlags,
				blockSize)
        {
        }
        public ZInputStream(Stream input, bool nowrap)
            : this()
        {
            Debug.Assert(input.CanRead);

            this._input = input;
            this._compressor = new Inflate(nowrap);
            this._compressionMode = CompressionMode.Decompress;
            this._compressor.next_in = _buffer;
            this._compressor.next_in_index = 0;
            this._compressor.avail_in = 0;
        }
 public DeflateStream(Stream stream, CompressionMode mode) :
     this(stream, mode, false, false)
 {
 }
 public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen) :
     this(stream, mode, leaveOpen, false)
 {
 }