public IEnumerable <InputStream> Read(string inputFile, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(inputFile)) { throw new ArgumentException("Input file name must be non-empty"); } List <InputStream> inputStreams = new List <InputStream>(); MemoryMappedFile memoryMappedFile = MemoryMappedFile.CreateFromFile(inputFile); MemoryMappedViewStream stream = memoryMappedFile.CreateViewStream(0, 1024); IBlockReader blockReader = new BlockReader(); Block block = blockReader.Read(stream, BlockFlags.ExtraField); if (block.ExtraField != null && block.ExtraField.Length > 0) { GZipMultiStreamHeader multiStreamHeader = new GZipMultiStreamHeader(); multiStreamHeader.Deserialize(block.ExtraField); int streamIndex = 0; long offset = 0; foreach (MultiStreamHeaderItem multiStreamHeaderItem in multiStreamHeader.Items) { if (cancellationToken.IsCancellationRequested) { break; } InputStream inputStream = ReadStream(streamIndex++, memoryMappedFile, offset, multiStreamHeaderItem.Length); offset += multiStreamHeaderItem.Length; inputStreams.Add(inputStream); } } else { FileInfo fileInfo = new FileInfo(inputFile); long size = fileInfo.Length; int chunkSize = _settings.ChunkSize; if (size < chunkSize) { InputStream inputStream = new InputStream(0); IStreamChunk inputStreamChunk = new StreamChunk(0, fileInfo.Open(FileMode.Open)); inputStream.Chunks.Add(inputStreamChunk); inputStreams.Add(inputStream); } else { InputStream inputStream = ReadStream(0, memoryMappedFile, 0, size); inputStreams.Add(inputStream); } } return(inputStreams); }
public void TestCanDeserializeSerializedHeader() { MultiStreamHeaderItem[] items = new[] { new MultiStreamHeaderItem { Length = ushort.MaxValue }, new MultiStreamHeaderItem { Length = ushort.MaxValue }, new MultiStreamHeaderItem { Length = ushort.MaxValue }, new MultiStreamHeaderItem { Length = ushort.MaxValue / 2 } }; GZipMultiStreamHeader header = new GZipMultiStreamHeader(); foreach (MultiStreamHeaderItem item in items) { header.Items.Add(item); } byte[] buffer = header.Serialize(); GZipMultiStreamHeader newHeader = new GZipMultiStreamHeader(); newHeader.Deserialize(buffer); Assert.AreEqual(items.Length, newHeader.Items.Count, "Counts of items are not equal"); for (int i = 0; i < items.Length; i++) { Assert.AreEqual( items[i], newHeader.Items[i], string.Format("Items # {0} are not equal", i + 1) ); } }