public DualBinaryStore(IStreamProvider provider, long start, long maxLength) { if (provider == null) throw new ArgumentNullException("provider"); if (start < 0) throw new ArgumentException("start parameter must have a value equal or greater than 0"); if (maxLength <= 0) throw new ArgumentException("maxLength parameter must have a value greater than 0"); _provider = provider; _start = start; _maxLength = maxLength; _writeHeaderStream = provider.AcquireWriteStream(); _writeLeftStream = provider.AcquireWriteStream(); _writeRightStream = provider.AcquireWriteStream(); _writeHeaderStream.Seek(start, SeekOrigin.Begin); if (_writeHeaderStream.Length > start) { using (var readStream = _provider.AcquireReadStream()) { readStream.Seek(start, SeekOrigin.Begin); var headerBuffer = new byte[16]; readStream.Read(headerBuffer, 0, headerBuffer.Length); _leftOffset = BitConverter.ToInt64(headerBuffer, 0); _rightOffset = BitConverter.ToInt64(headerBuffer, 8); // If the buffer was not generated by the store if (_leftOffset <= 0) _leftOffset = 16; if (_rightOffset <= 0) _rightOffset = maxLength; } _writeLeftStream.Seek(start + _leftOffset, SeekOrigin.Begin); _writeRightStream.Seek(start + _rightOffset, SeekOrigin.Begin); } else { _leftOffset = 16; _rightOffset = maxLength; var headerBuffer = BitConverter.GetBytes(_leftOffset); _writeHeaderStream.Write(headerBuffer, 0, headerBuffer.Length); headerBuffer = BitConverter.GetBytes(_rightOffset); _writeHeaderStream.Write(headerBuffer, 0, headerBuffer.Length); _writeHeaderStream.Seek(-16, SeekOrigin.Current); _writeRightStream.Seek(start + _leftOffset, SeekOrigin.Begin); var buffer = new byte[maxLength - _leftOffset]; _writeRightStream.Write(buffer, 0, buffer.Length); _writeLeftStream.Seek(start + _leftOffset, SeekOrigin.Begin); } _lastFlushedLeftOffset = _leftOffset; _lastFlushedRightOffset = _rightOffset; _left = new LeftBinaryStoreSegment(this); _right = new RightBinaryStoreSegment(this); }
public byte[] ReadAll(out long offset) { offset = 0; EnsureFlushed(_currentOffset); if (_currentOffset <= _start + 8) { return new byte[] { } } ; var buffer = new byte[_currentOffset - _start - 8]; using (var readStream = _provider.AcquireReadStream()) { readStream.Seek(_start + 8, SeekOrigin.Begin); readStream.Read(buffer, 0, buffer.Length); } return(buffer); }
private BinaryStore(IStreamProvider provider, long start, long length, bool canGrow) { _writeStream = provider.AcquireWriteStream(); _provider = provider; _start = start; _length = length; _canGrow = canGrow; if (_writeStream.Length > start) { using (var readStream = _provider.AcquireReadStream()) { readStream.Seek(start, SeekOrigin.Begin); var offsetBuffer = new byte[8]; readStream.Read(offsetBuffer, 0, offsetBuffer.Length); _currentOffset = BitConverter.ToInt64(offsetBuffer, 0); } } else { _currentOffset = 8; var offsetBuffer = BitConverter.GetBytes(_currentOffset); _writeStream.Seek(start, SeekOrigin.Begin); _writeStream.Write(offsetBuffer, 0, offsetBuffer.Length); if (length > 0) { var requiredFileSize = _start + _length; var buffer = new byte[requiredFileSize - _writeStream.Length - 8]; _writeStream.Write(buffer, 0, buffer.Length); } } _lastFlushOffset = _currentOffset; _writeStream.Seek(start + _currentOffset, SeekOrigin.Begin); _offsetWriteStream = provider.AcquireWriteStream(); _offsetWriteStream.Seek(start, SeekOrigin.Begin); }
private BinaryStore(IStreamProvider provider, long start, long maxLength) { _writeStream = provider.AcquireWriteStream(); _provider = provider; _start = start; _maxLength = maxLength; if (_writeStream.Length > start) { using (var readStream = _provider.AcquireReadStream()) { readStream.Seek(start, SeekOrigin.Begin); var offsetBuffer = new byte[8]; readStream.Read(offsetBuffer, 0, offsetBuffer.Length); _currentOffset = BitConverter.ToInt64(offsetBuffer, 0); } } else { _currentOffset = 8; var offsetBuffer = BitConverter.GetBytes(_currentOffset); _writeStream.Seek(start, SeekOrigin.Begin); _writeStream.Write(offsetBuffer, 0, offsetBuffer.Length); if (maxLength > 0) { var requiredFileSize = _start + _maxLength; var buffer = new byte[requiredFileSize - _writeStream.Length - 8]; _writeStream.Write(buffer, 0, buffer.Length); } } _lastFlushOffset = _currentOffset; _writeStream.Seek(start + _currentOffset, SeekOrigin.Begin); _offsetWriteStream = provider.AcquireWriteStream(); _offsetWriteStream.Seek(start, SeekOrigin.Begin); }