Пример #1
0
        private IEnumerable <DriveBlock> readAvailableContentBlocks()
        {
            lock (_entriesTableLock)
            {
                var operation = _synchronizer.EnqueueOperation(drive =>
                {
                    if (drive.Length < ByteHelper.GetLength <int>())
                    {
                        return(new byte[0]);
                    }

                    var buffer     = new byte[ByteHelper.GetLength <int>()];
                    drive.Position = drive.Length - ByteHelper.GetLength <int>() * 2;// read last 4 but 4 bytes

                    drive.Read(buffer, 0, buffer.Length);

                    var infoLength = BitConverter.ToInt32(buffer, 0);
                    if (infoLength <= 0)
                    {
                        buffer = new byte[0];
                    }
                    else
                    {
                        buffer = new byte[infoLength];

                        drive.Position = drive.Length - ByteHelper.GetLength <int>() * 2 - infoLength;
                        drive.Read(buffer, 0, infoLength);
                    }

                    drive.SetLength(drive.Length - infoLength - ByteHelper.GetLength <int>() * 2); //get rid of available blocks info
                    return(buffer);
                }, OperationType.FileTable);

                _synchronizer.EnqueueOperation(operation);
                var readBytes = operation.Task.Result;

                var processedBytes = 0;
                while (processedBytes < readBytes.Length)
                {
                    var retv = AvailableDriveBlock.Read(readBytes.Skip(processedBytes).Take(AvailableDriveBlock.BytesBlockLength)
                                                        .ToArray());

                    processedBytes += AvailableDriveBlock.BytesBlockLength;

                    yield return(retv);
                }
            }
        }
        public static AvailableDriveBlock Read(byte[] block)
        {
            if (block.Length != BytesBlockLength)
            {
                throw new InvalidOperationException("Unable to read block");
            }

            var position      = 0;
            var blockPosition = BitConverter.ToInt64(block, position);

            position += ByteHelper.GetLength <long>();
            var blockLength = BitConverter.ToInt32(block, position);
            var retv        = new AvailableDriveBlock(blockPosition, blockLength);

            return(retv);
        }