コード例 #1
0
ファイル: ZlibStreamTest.cs プロジェクト: alexcmd/DiscUtils
        public void TestInvalidChecksum()
        {
            byte[] testData = Encoding.ASCII.GetBytes("This is a test string");

            MemoryStream compressedStream = new MemoryStream();

            using (ZlibStream zs = new ZlibStream(compressedStream, CompressionMode.Compress, true))
            {
                zs.Write(testData, 0, testData.Length);
            }

            compressedStream.Seek(-2, SeekOrigin.End);
            compressedStream.Write(new byte[] { 0, 0 }, 0, 2);

            compressedStream.Position = 0;
            using (ZlibStream uzs = new ZlibStream(compressedStream, CompressionMode.Decompress, true))
            {
                byte[] outData = new byte[testData.Length];
                uzs.Read(outData, 0, outData.Length);
                Assert.AreEqual(testData, outData);

                // Should be end of stream
                Assert.AreEqual(-1, uzs.ReadByte());
            }
        }
コード例 #2
0
        public void TestRoundtrip()
        {
            byte[] testData = Encoding.ASCII.GetBytes("This is a test string");

            MemoryStream compressedStream = new MemoryStream();

            using (ZlibStream zs = new ZlibStream(compressedStream, CompressionMode.Compress, true))
            {
                zs.Write(testData, 0, testData.Length);
            }

            compressedStream.Position = 0;
            using (ZlibStream uzs = new ZlibStream(compressedStream, CompressionMode.Decompress, true))
            {
                byte[] outData = new byte[testData.Length];
                uzs.Read(outData, 0, outData.Length);
                Assert.AreEqual(testData, outData);

                // Should be end of stream
                Assert.AreEqual(-1, uzs.ReadByte());
            }
        }
コード例 #3
0
ファイル: MetablockWriter.cs プロジェクト: JGTM2016/discutils
        private void NextBlock()
        {
            MemoryStream compressed = new MemoryStream();
            using (ZlibStream compStream = new ZlibStream(compressed, CompressionMode.Compress, true))
            {
                compStream.Write(_currentBlock, 0, _currentOffset);
            }

            byte[] writeData;
            ushort writeLen;
            if (compressed.Length < _currentOffset)
            {
                writeData = compressed.GetBuffer();
                writeLen = (ushort)compressed.Length;
            }
            else
            {
                writeData = _currentBlock;
                writeLen = (ushort)(_currentOffset | 0x8000);
            }

            byte[] header = new byte[2];
            Utilities.WriteBytesLittleEndian(writeLen, header, 0);
            _buffer.Write(header, 0, 2);
            _buffer.Write(writeData, 0, writeLen & 0x7FFF);

            ++_currentBlockNum;
        }
コード例 #4
0
        /// <summary>
        /// Writes a block of file data, possibly compressing it.
        /// </summary>
        /// <param name="buffer">The data to write.</param>
        /// <param name="offset">Offset of the first byte to write.</param>
        /// <param name="count">The number of bytes to write.</param>
        /// <returns>
        /// The 'length' of the (possibly compressed) data written, including
        /// a flag indicating compression (or not).
        /// </returns>
        private uint WriteDataBlock(byte[] buffer, int offset, int count)
        {
            MemoryStream compressed = new MemoryStream();
            using (ZlibStream compStream = new ZlibStream(compressed, CompressionMode.Compress, true))
            {
                compStream.Write(buffer, offset, count);
            }

            byte[] writeData;
            int writeOffset;
            int writeLen;
            if (compressed.Length < count)
            {
                writeData = compressed.GetBuffer();
                writeOffset = 0;
                writeLen = (int)compressed.Length;
            }
            else
            {
                writeData = buffer;
                writeOffset = offset;
                writeLen = count | 0x01000000;
            }

            _context.RawStream.Write(writeData, writeOffset, writeLen & 0xFFFFFF);

            return (uint)writeLen;
        }