protected FileChunkProducer(string fileName, FileSplitInfo fileSplitInfo, FileDataHolder fileDataHolder, FileDataReader fileDataReader, FileDataWriter fileDataWriter)
     : base(fileName)
 {
     _fileSplitInfo = fileSplitInfo;
     _fileDataHolder = fileDataHolder;
     _fileDataReader = fileDataReader;
     _fileDataWriter = fileDataWriter;
 }
 public static ArchiveHeader Create(FileSplitInfo fileSplitInfo)
 {
     var header = new ArchiveHeader(fileSplitInfo.ChunkCount);
     for (int id = 0; id < fileSplitInfo.ChunkCount; id++)
     {
         header.ChunkLengths[id] = fileSplitInfo.Chunks[id].Length;
     }
     return header;
 }
Exemple #3
0
        public async Task SizeOfChunksSplitTest(string testCase, FileSplitInfo fileSplittingInfo, Exception expectedException)
        {
            try
            {
                var splitter = new SizeOfChunksSplitter(Configuration)
                {
                    FileSplittingInfo = fileSplittingInfo
                };
                await splitter.Split();

                Assert.NotNull(testCase);

                FileInfo inputFile      = new FileInfo(fileSplittingInfo.FilePath);
                long     totalBytes     = 0;
                int      numberOfChunks = (int)Math.Ceiling((double)inputFile.Length / fileSplittingInfo.ChunkSize);
                if (numberOfChunks > 1)
                {
                    for (int i = 0; i < numberOfChunks; i++)
                    {
                        string   chunkFileName = $"{inputFile.DirectoryName}{Path.DirectorySeparatorChar}{inputFile.Name}.part_{i + 1}";
                        FileInfo chunkFileInfo = new FileInfo(chunkFileName);

                        Assert.True(File.Exists(chunkFileName), $"{testCase} failed: file chunk doesn't exist '{chunkFileName}'");

                        totalBytes += chunkFileInfo.Length;
                    }
                }
                else
                {
                    totalBytes = inputFile.Length;
                }

                Assert.Equal(inputFile.Length, totalBytes);
            }
            catch (Exception ex)
            {
                if (expectedException == null)
                {
                    throw;
                }

                Assert.Equal(expectedException.GetType().FullName, ex.GetType().FullName);
                Assert.Equal(expectedException.Message, ex.Message);
            }
            finally
            {
                DirectoryInfo dirInfoParts = new DirectoryInfo(fileSplittingInfo.DestinationPath);
                foreach (var file in dirInfoParts.GetFiles("SizeOfChunksSplitterTests_testFile_*.part_*"))
                {
                    File.Delete(file.FullName);
                }
            }
        }
 public FileSplitInfo GetFileSplitInfo()
 {
     var chunkLengths = GetChunkLengths();
     var splitInfo = new FileSplitInfo(chunkLengths.Length);
     for (long id = 0, position = 0; id < splitInfo.ChunkCount; id++)
     {
         var fileChunkInfo = new FileChunkInfo(id, chunkLengths[id])
         {
             Position = position
         };
         splitInfo.Chunks[id] = fileChunkInfo;
         position += fileChunkInfo.Length;
     }
     return splitInfo;
 }
Exemple #5
0
        public void BuildFileSplitInfoTest(string testCase, string[] arguments, FileSplitInfo expectedResult, Exception expectedException)
        {
            try
            {
                var argumentParser = new ArgumentParser()
                {
                    Arguments = arguments
                };
                var actual = argumentParser.BuildFileSplitInfo();

                Assert.NotNull(testCase);
                Assert.Equal(expectedResult.FilePath, actual.FilePath);
                Assert.Equal(expectedResult.NumberOfChunks, actual.NumberOfChunks);
                Assert.Equal(expectedResult.ChunkSize, actual.ChunkSize);
            }
            catch (Exception ex)
            {
                Assert.Equal(expectedException.GetType().FullName, ex.GetType().FullName);
                Assert.Equal(expectedException.Message, ex.Message);
            }
        }
Exemple #6
0
        public FileSplitInfo BuildFileSplitInfo()
        {
            FileSplitInfo fileSplitInfo = null;

            ArgumentInfo filePathArgument        = SplitOptionsEnum.FilePath.GetAttribute <ArgumentInfo>();
            ArgumentInfo destinationPathArgument = SplitOptionsEnum.DestinationPath.GetAttribute <ArgumentInfo>();
            ArgumentInfo numberOfChunksArgument  = SplitOptionsEnum.NumberOfChunks.GetAttribute <ArgumentInfo>();
            ArgumentInfo chunkSizeArgument       = SplitOptionsEnum.ChunkSize.GetAttribute <ArgumentInfo>();

            var recognisedSwitches = new List <ArgumentInfo>()
            {
                filePathArgument, destinationPathArgument, numberOfChunksArgument, chunkSizeArgument
            };

            var switchesInArgs           = Arguments.Where(a => a.StartsWith("-")).Skip(1).ToList();
            var recognisedSwitchesInArgs = switchesInArgs.Where(a => recognisedSwitches.Select(x => x.ArgumentSwitch).Contains(a)).ToList();

            if (recognisedSwitchesInArgs.Count != switchesInArgs.Count)
            {
                throw new FileSplitterMergerException($"Unrecognised split option: {string.Join(", ", switchesInArgs.Except(recognisedSwitchesInArgs))}");
            }

            if ((switchesInArgs?.Count ?? 0) > (switchesInArgs?.Select(x => x?.ToLower())?.Distinct()?.Count() ?? 0))
            {
                throw new FileSplitterMergerException($"Duplicated options");
            }

            string filePath = GetArgument(filePathArgument.ArgumentSwitch);

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new FileSplitterMergerException($"{filePathArgument.ArgumentDescription} not specified");
            }

            string destinationPath = GetArgument(destinationPathArgument.ArgumentSwitch);

            if (string.IsNullOrWhiteSpace(destinationPath))
            {
                throw new FileSplitterMergerException($"{destinationPathArgument.ArgumentDescription} not specified");
            }

            string numberOfChunksString    = GetArgument(numberOfChunksArgument.ArgumentSwitch);
            bool   numberOfChunksSpecified = int.TryParse(numberOfChunksString, out int numberOfChunks);

            string chunkSizeString       = GetArgument(chunkSizeArgument.ArgumentSwitch);
            bool   sizeOfChunksSpecified = long.TryParse(chunkSizeString, out long chunkSize);

            if (!numberOfChunksSpecified && !sizeOfChunksSpecified ||
                (numberOfChunks <= 0 && chunkSize <= 0) ||
                (numberOfChunks > 0 && chunkSize > 0))
            {
                throw new FileSplitterMergerException($"Please specify either {numberOfChunksArgument.ArgumentDescription.ToLower()} or {chunkSizeArgument.ArgumentDescription.ToLower()}");
            }

            if (numberOfChunks > 0)
            {
                fileSplitInfo = new FileSplitInfo(filePath, numberOfChunks, destinationPath);
            }
            else if (chunkSize > 0)
            {
                fileSplitInfo = new FileSplitInfo(filePath, destinationPath, chunkSize);
            }

            return(fileSplitInfo);
        }