public void TryExpandingTheStreamViaSetLengthAndThenWritingReadingFromIt()
        {
            DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream();

            const int testArrayLength = 500000;

            dataStream.SetLength(testArrayLength);

            Assert.AreEqual(testArrayLength, dataStream.Length);
            Assert.AreEqual(0, dataStream.Position);

            const int halfTestArray = testArrayLength / 2; //на половине пути запишем что-нибудь интересное.

            dataStream.SetPosition(halfTestArray);

            var bytesToWrite = new byte[halfTestArray];

            for (int i = 0; i < bytesToWrite.Length; i++)
            {
                bytesToWrite[i] = 231;
            }

            dataStream.Write(bytesToWrite, 0, bytesToWrite.Length);

            dataStream.SetPosition(halfTestArray);

            var bytesRead = new byte[bytesToWrite.Length];

            dataStream.Read(bytesRead, 0, bytesRead.Length);

            MakeSureEachByteInArrayIsRight(bytesRead, 231); // и проверим, что записалось что нужно, - новый объем как будто проработан.
        }
        public void MakeSureShrinkingFreesDiskSpace()
        {
            var testCollaborators = TestCollaboratorsFactory.CreateCollaboratorsForTestingDataStreams();

            DataStreamReadableWritable dataStream =
                testCollaborators.VirtualFileSystem.OpenFileForWriting(testCollaborators.FileInfo.FullPath);

            int valueCloseToDiskSize = (int)testCollaborators.Disk.DiskSizeInBytes - 25 * testCollaborators.Disk.BlockSizeInBytes;

            dataStream.SetLength(valueCloseToDiskSize);

            var fileSystem = testCollaborators.VirtualFileSystem;

            FileInfo newFileNode = fileSystem.CreateFile(@"\testFile2");

            DataStreamReadableWritable newFileNodeStream = fileSystem.OpenFileForWriting(newFileNode.FullPath);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <InsufficientSpaceException>(
                delegate
            {
                newFileNodeStream.SetLength(valueCloseToDiskSize);
            });

            dataStream.SetLength(0);

            newFileNodeStream.SetLength(valueCloseToDiskSize);
            dataStream.SetPosition(0);

            const int answeringToAllQuestionsByte = 42;

            byte[] bytesToWrite = ByteBufferFactory.CreateByteBufferWithAllBytesSet(valueCloseToDiskSize, answeringToAllQuestionsByte);

            newFileNodeStream.Write(bytesToWrite, 0, bytesToWrite.Length);

            newFileNodeStream.SetPosition(0);

            var buffer = new byte[valueCloseToDiskSize];

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

            MakeSureEachByteInArrayIsRight(buffer, answeringToAllQuestionsByte);
        }
        private void ShrinkAndMakeSureDataIsIntact(DataStreamReadableWritable dataStream, int newLength)
        {
            dataStream.SetLength(newLength);

            Assert.AreEqual(newLength, dataStream.Length);

            dataStream.SetPosition(0);

            byte[] bytesRead = new byte[newLength];

            dataStream.Read(bytesRead, 0, bytesRead.Length);

            for (int i = 0; i < newLength; i++)
            {
                Assert.AreEqual(bytesRead[i], (byte)(i / 10000));
            }
        }
        public void TryShrinkingTheStreamAndThenWritingReadingFromIt()
        {
            DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream();

            const int testArrayLength = 50000;

            dataStream.SetLength(testArrayLength);

            byte[] bytesToWrite = new byte[testArrayLength];

            for (int i = 0; i < bytesToWrite.Length; i++)
            {
                bytesToWrite[i] = (byte)(i / 10000);
            }

            dataStream.Write(bytesToWrite, 0, bytesToWrite.Length);

            ShrinkAndMakeSureDataIsIntact(dataStream, 40000);
            ShrinkAndMakeSureDataIsIntact(dataStream, 34567);
            ShrinkAndMakeSureDataIsIntact(dataStream, 25000);
            ShrinkAndMakeSureDataIsIntact(dataStream, 9999);
            ShrinkAndMakeSureDataIsIntact(dataStream, 24);
            ShrinkAndMakeSureDataIsIntact(dataStream, 1);
        }
        /// <summary>
        /// Устанавливает длину потока (беря себе или освобождая соответствующий объем дискового пространства).
        /// </summary>
        /// <param name="newLength">Новая длина потока</param>
        /// <exception cref="InsufficientSpaceException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        public override void SetLength(int newLength)
        {
            _dataStream.SetLength(newLength);

            this.PersistOwningNode();
        }