public IEnumerable<PartitionedFileStream> GetStreams()
        {
            _fileStream.Seek(0, SeekOrigin.Begin);
            var header = new PartitionedFileHeader(0);
            header.Read(_fileStream);

            var currentIndex = PartitionedFileHeader.DataOffset;
            var dataStart = currentIndex;
            var allOffsets = new List<long>();
            while (currentIndex + sizeof(long) < dataStart + header.LengthOfCurrentBuffer)
            {
                var partitionedFileStreamOffset = Serializer.DeserializeLong(_fileStream);
                allOffsets.Add(partitionedFileStreamOffset);
            }

            var allStreams = new List<PartitionedFileStream>();
            foreach (var offset in allOffsets)
            {
                header = new PartitionedFileHeader(offset);
                _fileStream.Seek(offset, SeekOrigin.Begin);
                header.Read(_fileStream);

                allStreams.Add(new PartitionedFileStream(_fileStream, header, AddNewHeaderSection));
            }

            return allStreams;
        }
 public PartitionedFileStreamFactory(string fileName)
 {
     _fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
     if (_fileStream.Length == 0)
     {
         _header = new PartitionedFileHeader(0);
         _dataStart = _newStreamOffset = PartitionedFileHeader.DataOffset;
         _header.Write(_fileStream);
     }
 }
Ejemplo n.º 3
0
        public PartitionedFileStream(FileStream fileStream, PartitionedFileHeader header, Func<PartitionedFileHeader> getNewHeader)
        {
            _fileStream = fileStream;
            _header = header;
            _getNewHeader = getNewHeader;

            UpdateLength();

            SeekFromBegin(0);

            _position = 0;
        }
        public PartitionedFileStream GetNewStream()
        {
            var bufferOverrun = (_newStreamOffset + sizeof (long)) - (_dataStart + _header.LengthOfCurrentBuffer);
            if (bufferOverrun > 0)
            {
                _header.OffsetOfNextHeader = _fileStream.Length;
                _fileStream.Seek(_currentHeaderOffset, SeekOrigin.Begin);
                _header.Write(_fileStream);
                _currentHeaderOffset = _header.OffsetOfHeader;

                _header = AddNewHeaderSection();

                _header.OffsetOfPrevHeader = _currentHeaderOffset;
                _fileStream.Seek(_header.OffsetOfHeader, SeekOrigin.Begin);
                _header.Write(_fileStream);
            }

            _fileStream.Seek(_newStreamOffset, SeekOrigin.Begin);
            var newHeader = AddNewHeaderSection();
            Serializer.Serialize(_fileStream, _fileStream.Length);
            _newStreamOffset += sizeof (long);

            return new PartitionedFileStream(_fileStream, newHeader, AddNewHeaderSection);
        }
Ejemplo n.º 5
0
            public PartitionedFileHeader AddNewHeaderSection()
            {
                var header = new PartitionedFileHeader(_fileStream.Length) { LengthOfCurrentBuffer = _bufferSize };
                _fileStream.Seek(0, SeekOrigin.End);
                header.Write(_fileStream);

                var buffer = new byte[_bufferSize];
                _fileStream.Write(buffer, 0, buffer.Length);

                _fileStream.Seek(PartitionedFileHeader.DataOffset, SeekOrigin.Begin);

                return header;
            }
Ejemplo n.º 6
0
        private void MoveToPreviousHeader()
        {
            if (_header.OffsetOfPrevHeader == null)
                throw new InvalidOperationException("Already at the beginning of the Stream");

            var oldPosition = _fileStream.Position;
            _fileStream.Seek(_header.OffsetOfPrevHeader.Value, SeekOrigin.Begin);
            _header = new PartitionedFileHeader(_header.OffsetOfPrevHeader.Value);
            _header.Read(_fileStream);
            _fileStream.Seek(oldPosition, SeekOrigin.Begin);
        }
Ejemplo n.º 7
0
        private void MoveToNextHeader()
        {
            var oldPosition = _fileStream.Position;
            if (_header.OffsetOfNextHeader == null)
            {
                var nextHeader = _getNewHeader();
                _header.OffsetOfNextHeader = nextHeader.OffsetOfHeader;
                _fileStream.Seek(_header.OffsetOfHeader, SeekOrigin.Begin);
                _header.Write(_fileStream);

                nextHeader.OffsetOfPrevHeader = _header.OffsetOfHeader;
                _fileStream.Seek(nextHeader.OffsetOfHeader, SeekOrigin.Begin);
                nextHeader.Write(_fileStream);

                _header = nextHeader;
            }
            else
            {
                _fileStream.Seek(_header.OffsetOfNextHeader.Value, SeekOrigin.Begin);
                _header = new PartitionedFileHeader(_header.OffsetOfNextHeader.Value);
                _header.Read(_fileStream);
            }
            _fileStream.Seek(oldPosition, SeekOrigin.Begin);
        }
        private PartitionedFileHeader AddNewHeaderSection()
        {
            const int bufferSize = 4096;
            var header = new PartitionedFileHeader(_fileStream.Length) {LengthOfCurrentBuffer = bufferSize};
            _fileStream.Seek(0, SeekOrigin.End);
            header.Write(_fileStream);

            var buffer = new byte[bufferSize];
            _fileStream.Write(buffer, 0, buffer.Length);

            return header;
        }