/// <summary> /// /// </summary> /// <param name="objectToWrite"></param> /// <param name="destinationDiskBlock"></param> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <exception cref="InconsistentDataDetectedException"></exception> private void WriteObject(object objectToWrite, int destinationDiskBlock) { try { MemoryStream memoryStream = SerializeToMemoryStream(objectToWrite); var bytesToWrite = new byte[memoryStream.Length + 4]; var finalStream = new MemoryStream(bytesToWrite, true); var writer = new BinaryWriter(finalStream); writer.Write((Int32)memoryStream.Length); writer.Write(memoryStream.ToArray()); _virtualDisk.WriteBytesToBlock(destinationDiskBlock, finalStream.ToArray()); } catch (SerializationException exception) { throw ConstructGenericCannotWriteNodeException(exception); } catch (ArgumentException exception) { throw ConstructGenericCannotWriteNodeException(exception); } catch (InvalidOperationException exception) { throw ConstructGenericCannotWriteNodeException(exception); } }
/// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <exception cref="InvalidOperationException"></exception> public void WriteBytes(byte[] array, int startingPosition, int length) { MethodArgumentValidator.ThrowIfNull(array, "array"); MethodArgumentValidator.ThrowIfNegative(startingPosition, "startingPosition"); MethodArgumentValidator.ThrowIfNegative(length, "length"); if ((startingPosition + length) > array.Length) { throw new ArgumentException("В массиве, начиная с позиции {0}, не найдется следующего числа байт для записи: {1}".FormatWith(startingPosition, length)); } if (this.IsAtEndOfBlock) { throw new InvalidOperationException("Запись за пределами границ блока не разрешена"); } int positionAfterWriting = this.Position + length; if (positionAfterWriting > this.SizeInBytes) { throw new ArgumentException("Запись невозможна: попытка записи за пределами массива"); } _disk.WriteBytesToBlock(BlockIndex, array, startingPosition, _position, length); if ((positionAfterWriting > this.OccupiedSpaceInBytes) && (this.OccupiedSpaceInBytes < this.SizeInBytes)) { this.OccupiedSpaceInBytes += (positionAfterWriting - this.OccupiedSpaceInBytes); } this.Position = positionAfterWriting; }