Example #1
0
        private void ReadBlocks(EndianBinaryReader reader, Stream blocksStream)
        {
            foreach (var blockInfo in m_BlocksInfo)
            {
                var compressionType = (CompressionType)(blockInfo.flags & StorageBlockFlags.CompressionTypeMask);
                switch (compressionType)
                {
                case CompressionType.None:
                {
                    reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize);
                    break;
                }

                case CompressionType.Lzma:
                {
                    SevenZipHelper.StreamDecompress(reader.BaseStream, blocksStream, blockInfo.compressedSize, blockInfo.uncompressedSize);
                    break;
                }

                case CompressionType.Lz4:
                case CompressionType.Lz4HC:
                {
                    var compressedSize  = (int)blockInfo.compressedSize;
                    var compressedBytes = BigArrayPool <byte> .Shared.Rent(compressedSize);

                    reader.Read(compressedBytes, 0, compressedSize);
                    var uncompressedSize  = (int)blockInfo.uncompressedSize;
                    var uncompressedBytes = BigArrayPool <byte> .Shared.Rent(uncompressedSize);

                    var numWrite = LZ4Codec.Decode(compressedBytes, 0, compressedSize, uncompressedBytes, 0, uncompressedSize);
                    if (numWrite != uncompressedSize)
                    {
                        throw new IOException($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes");
                    }
                    blocksStream.Write(uncompressedBytes, 0, uncompressedSize);
                    BigArrayPool <byte> .Shared.Return(compressedBytes);

                    BigArrayPool <byte> .Shared.Return(uncompressedBytes);

                    break;
                }

                default:
                    throw new IOException($"Unsupported compression type {compressionType}");
                }
            }
            blocksStream.Position = 0;
        }
Example #2
0
        private void ReadBlocks(EndianBinaryReader reader, Stream blocksStream)
        {
            foreach (var blockInfo in m_BlocksInfo)
            {
                switch (blockInfo.flags & 0x3F) //kStorageBlockCompressionTypeMask
                {
                default:                        //None
                {
                    reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize);
                    break;
                }

                case 1:     //LZMA
                {
                    SevenZipHelper.StreamDecompress(reader.BaseStream, blocksStream, blockInfo.compressedSize, blockInfo.uncompressedSize);
                    break;
                }

                case 2:     //LZ4
                case 3:     //LZ4HC
                {
                    var compressedSize  = (int)blockInfo.compressedSize;
                    var compressedBytes = BigArrayPool <byte> .Shared.Rent(compressedSize);

                    reader.Read(compressedBytes, 0, compressedSize);
                    var uncompressedSize  = (int)blockInfo.uncompressedSize;
                    var uncompressedBytes = BigArrayPool <byte> .Shared.Rent(uncompressedSize);

                    var numWrite = LZ4Codec.Decode(compressedBytes, 0, compressedSize, uncompressedBytes, 0, uncompressedSize);
                    if (numWrite != uncompressedSize)
                    {
                        throw new IOException($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes");
                    }
                    blocksStream.Write(uncompressedBytes, 0, uncompressedSize);
                    BigArrayPool <byte> .Shared.Return(compressedBytes);

                    BigArrayPool <byte> .Shared.Return(uncompressedBytes);

                    break;
                }
                }
            }
            blocksStream.Position = 0;
        }