public BlockCompressor(BlockWriter writer, int blockIndex, int uncompressedBlockSize, byte[] uncompressedBlockData) { _writer = writer; BlockIndex = blockIndex; SourceBlockSize = uncompressedBlockSize; SourceBlockData = new byte[uncompressedBlockSize]; SourceBlockData = uncompressedBlockData.ToArray(); }
public BlockDecompressor(BlockWriter writer, int blockIndex, int compressedBlockSize, byte[] compressedBlockData, int defaultUncompressedBlockSize) { _writer = writer; BlockIndex = blockIndex; SourceBlockSize = compressedBlockSize; SourceBlockData = compressedBlockData.ToArray(); _defaultBlockSize = defaultUncompressedBlockSize; }
private Result Decompress(string sourceFileName, string destinationFileName) { try { using (ChunkManager chunkManager = new ChunkManager(_processorCount)) { using (var compressedReader = new CompressedReader(sourceFileName, _marker)) { using (var processedWriter = new BlockWriter(_marker, destinationFileName)) { var readFirstResult = compressedReader.ReadFirst(); if (readFirstResult.IsFailure) { return(readFirstResult); } for (int i = 0; i < compressedReader.TotalBlockCount; i++) { var nextBlockResult = compressedReader.ReadNext(); if (nextBlockResult.IsFailure) { return(Result.Fail(nextBlockResult.Error)); } var nextBlock = nextBlockResult.Value; var workBlockForDecompression = new BlockDecompressor(processedWriter, nextBlock.BlockIndex, nextBlock.ActualReadSize, nextBlock.ReadBlockData, compressedReader.MaxBlockSize); var startThreadResult = chunkManager.AddBlockAndTryToProcess(workBlockForDecompression); if (startThreadResult.IsFailure) { return(Result.Fail(startThreadResult.Error)); } chunkManager.StartUnstartedChunksAndCountRemained(); if (chunkManager.Status.IsFailure) { return(chunkManager.Status); } chunkManager.RemoveWrittenChunks(); } while (chunkManager.StartUnstartedChunksAndCountRemained() > 0) { Thread.Sleep(10); } chunkManager.WaitingUnfinishedChunks(); if (processedWriter.Status.IsFailure) { return(processedWriter.Status); } chunkManager.StartUnstartedChunksAndCountRemained(); if (chunkManager.Status.IsFailure) { return(chunkManager.Status); } } } } } catch (Exception ex) { return(Result.Fail($"Decompressing error: {ex.Message}")); } return(Result.Ok()); }
private Result Compress(string uncompressedFileName, string compressedFileName, int uncompressedBlockSize) { try { using (ChunkManager chunkManager = new ChunkManager(_processorCount)) { using (UncompressedReader uncompressedReader = new UncompressedReader(uncompressedFileName, uncompressedBlockSize)) { using (var processedWriter = new BlockWriter(_marker, compressedFileName)) { var writeHeaderResult = processedWriter.WriteHeader(uncompressedReader.TotalBlockCount, uncompressedBlockSize); if (writeHeaderResult.IsFailure) { return(writeHeaderResult); } while (true) { var readBlockResult = uncompressedReader.ReadNext(); if (readBlockResult.IsFailure) { return(Result.Fail(readBlockResult.Error)); } var readBlock = readBlockResult.Value; if (readBlock.ActualReadSize <= 0) { break; } var workBlockForProcessing = new BlockCompressor(processedWriter, readBlock.BlockIndex, readBlock.ActualReadSize, readBlockResult.Value.ReadBlockData); var startThreadResult = chunkManager.AddBlockAndTryToProcess(workBlockForProcessing); if (startThreadResult.IsFailure) { return(Result.Fail(startThreadResult.Error)); } if (processedWriter.Status.IsFailure) { return(processedWriter.Status); } chunkManager.StartUnstartedChunksAndCountRemained(); if (chunkManager.Status.IsFailure) { return(chunkManager.Status); } chunkManager.RemoveWrittenChunks(); } while (chunkManager.StartUnstartedChunksAndCountRemained() > 0) { Thread.Sleep(10); } chunkManager.WaitingUnfinishedChunks(); if (processedWriter.Status.IsFailure) { return(processedWriter.Status); } chunkManager.StartUnstartedChunksAndCountRemained(); if (chunkManager.Status.IsFailure) { return(chunkManager.Status); } } } } } catch (Exception ex) { return(Result.Fail($"Compressing error: {ex.Message}")); } return(Result.Ok()); }