public void Execute(OperationParameters parameters) { var inputStream = File.OpenRead(parameters.InputFileName); var outputStream = File.Create(parameters.OutputFileName); blockStreamWriter.Initialize(outputStream); int inputBlockSize = parameters.InputBlockSize; int blocksCount = (int)(inputStream.Length / inputBlockSize) + (inputStream.Length % inputBlockSize == 0 ? 0 : 1); int lastInputBlockSize = (int)(inputStream.Length - inputBlockSize * (blocksCount - 1)); bufferPool.Initialize(Math.Min(threadPool.WorkersCount, blocksCount) * 3, inputBlockSize); logger.Debug($"{nameof(CompressOperationExecutor)}. Number of blocks {blocksCount}"); outputStream.Write(BitConverter.GetBytes(inputStream.Length)); outputStream.Write(BitConverter.GetBytes(inputBlockSize)); for (int blockNumber = 0; blockNumber < blocksCount; blockNumber++) { var inputBlock = bufferPool.Take(); int currentInputBlockSize = blockNumber == blocksCount - 1 ? lastInputBlockSize : inputBlockSize; inputBlock.FillFrom(inputStream, currentInputBlockSize); var outputBlock = bufferPool.Take(); int number = blockNumber; threadPool.Enqueue(() => CompressBlock(inputBlock, number, outputBlock)); } inputStream.Close(); threadPool.Wait(); blockStreamWriter.Wait(blocksCount); outputStream.Close(); }
public void Execute(OperationParameters parameters) { var inputStream = File.OpenRead(parameters.InputFileName); var inputStreamReader = new BinaryReader(inputStream); long dataSize = inputStreamReader.ReadInt64(); int outputBlockSize = inputStreamReader.ReadInt32(); int blocksCount = (int)(dataSize / outputBlockSize) + (dataSize % outputBlockSize == 0 ? 0 : 1); int lastOutputBlockSize = (int)(dataSize - outputBlockSize * (blocksCount - 1)); logger.Debug($"{nameof(DecompressOperationExecutor)}. Number of blocks {blocksCount}"); var outputStream = File.Create(parameters.OutputFileName); blockStreamWriter.Initialize(outputStream); bufferPool.Initialize(Math.Min(threadPool.WorkersCount, blocksCount) * 3, outputBlockSize); for (int blockNumber = 0; blockNumber < blocksCount; blockNumber++) { var inputBlock = bufferPool.Take(); int inputBlockSize = inputStreamReader.ReadInt32(); inputBlock.FillFrom(inputStream, inputBlockSize); var outputBlock = bufferPool.Take(); int currentOutputBlockSize = blockNumber == blocksCount - 1 ? lastOutputBlockSize : outputBlockSize; int number = blockNumber; threadPool.Enqueue(() => DecompressBlock(inputBlock, number, outputBlock, currentOutputBlockSize)); logger.Debug($"{nameof(DecompressOperationExecutor)}. Block {number}: input size {inputBlockSize}, output size {outputBlockSize}"); } inputStream.Close(); threadPool.Wait(); blockStreamWriter.Wait(blocksCount); var resultDataSize = outputStream.Length; outputStream.Close(); if (dataSize != resultDataSize) { throw new InvalidDataException($"Decompressed data size is {resultDataSize}, but expected {dataSize}"); } }
public void Send(Message msg) { _sentMessagesMeter.Mark(); var buffer = _bufferPool.Take(_config.MaxMessageSizeBts); try { using (var stream = new MemoryStream(buffer)) { MessageSerializer.Serialize(msg, stream); TransportChannel.Send(msg.Priority, msg.Reliability, new ArraySegment <byte>(buffer, 0, (int)stream.Position)); } Logger.NetChannelSentMessage(Node, this, msg); } finally { _bufferPool.Return(buffer); } }
/// <summary> /// Allocate a buffer of at least <paramref name="size"/> bytes from the current <see cref="IBufferPool"/>. /// </summary> /// <param name="size">The minimum size required, in bytes.</param> /// <returns>A byte array having at least <paramref name="size"/> bytes.</returns> public static byte[] Take(int size) { return(s_bufferPool.Take(size)); }