/// <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;
        }
Exemplo n.º 2
0
        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;
        }