示例#1
0
        /// <summary>
        /// Sets compression level for compression stream. If the current stream was created as a decompression stream the <see cref="NotSupportedException" /> is thrown.
        /// </summary>
        /// <param name="level">An integer value indicating the compression level. The parameter can take values from 0 to 9. You can pass -1 as a parameter to use the default compression level.</param>
        /// <exception cref="ArgumentException">The <c>ArgumentException</c> exception is thrown if the specified compression level is less then -1 or greater than 9.</exception>
        /// <exception cref="NotSupportedException">The NotSupportedException exception is thrown if we call this method for the decompression stream.</exception>
        public void SetCompressionLevel(int level)
        {
            if (level < -1 || level > 9)
            {
                throw new ArgumentException("Invalid compression level is specified", "level");
            }

            if (this.compressionDirection == CompressionDirection.Decompression)
            {
                throw new NotSupportedException("The compression level cannot be set for decompression stream");
            }

            this.compLevel         = level;
            this.compressionStream = new ZOutputStream(this._stream, this.compLevel);
        }
示例#2
0
        /// <summary>
        /// Writes MLArrays into <c>OutputStream</c>
        /// </summary>
        /// <remarks>
        /// Writes MAT-file header and compressed data (<c>miCompressed</c>).
        /// </remarks>
        /// <param name="stream"><c>Stream</c></param>
        /// <param name="data"><c>Collection</c> of <c>MLArray</c> elements.</param>
        /// <param name="compress">Use data compression?</param>
        public MatFileWriter(BinaryWriter stream, ICollection data, bool compress)
        {
            // Write header
            WriteHeader(stream);

            foreach (MLArray matrix in data)
            {
                if (compress)
                {
                    // Prepare buffer for MATRIX data
                    MemoryStream memstrm = new MemoryStream();
                    BinaryWriter bw      = new BinaryWriter(memstrm);
                    WriteMatrix(bw, matrix);                  // Write MATRIX bytes into buffer
                    memstrm.Position = 0;                     // Rewind the stream

                    // Compress data to save storage
                    MemoryStream compressed = new MemoryStream();
#if NET20
                    zlib.ZOutputStream zos   = new zlib.ZOutputStream(compressed, -1);
                    byte[]             input = new byte[128];
                    int len = 0;
                    while ((len = memstrm.Read(input, 0, input.Length)) > 0)
                    {
                        zos.Write(input, 0, len);
                    }
                    zos.Flush();
                    zos.Close();  // important to note that the zlib.ZOutputStream needs to close before writting out the data to the Base.stream

                    byte[] compressedBytes = compressed.ToArray();
#endif
#if NET40
                    uint s1 = 1, s2 = 0, crc = 0;                     // Adler-32 CRC
                    using (DeflateStream df = new DeflateStream(compressed, CompressionMode.Compress, true))
                    {
                        int readByte;
                        do
                        {
                            readByte = memstrm.ReadByte();
                            if (readByte != -1)
                            {
                                df.WriteByte((byte)readByte);
                                s1 = (s1 + (byte)readByte) % 0xFFF1;
                                s2 = (s2 + s1) % 0xFFF1;
                            }
                        }while (readByte != -1);
                        crc = (s2 << 16) | s1;
                    }

                    compressed.Position = 0;

                    // zip RFC 1950
                    byte[] compressedBytes = new byte[compressed.Length + 6];
                    compressedBytes[0] = 0x78;
                    compressedBytes[1] = 0x9c;
                    for (int i = 2; i < compressedBytes.Length - 4; i++)
                    {
                        compressedBytes[i] = (byte)compressed.ReadByte();
                    }
                    BitConverter.GetBytes(crc).CopyTo(compressedBytes, compressedBytes.Length - 4);
#endif

                    // write COMPRESSED tag and compressed data into output channel
                    ByteBuffer buf         = new ByteBuffer(2 * 4 + compressedBytes.Length);
                    buf.PutInt(MatDataTypes.miCOMPRESSED);
                    buf.PutInt(compressedBytes.Length);
                    buf.Put(compressedBytes, 0, compressedBytes.Length);

                    stream.Write(buf.Array());

                    compressed.Close();
                }
                else
                {
                    // Write MATRIX bytes into buffer
                    WriteMatrix(stream, matrix);
                }
            }

            stream.Close();
        }