Ejemplo n.º 1
0
 /// <summary>
 /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
 /// </summary>
 /// <returns>
 /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
 /// </returns>
 /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source. </param><param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream. </param><param name="count">The maximum number of bytes to be read from the current stream. </param><exception cref="T:System.ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is larger than the buffer length. </exception><exception cref="T:System.ArgumentNullException"><paramref name="buffer"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negative. </exception><exception cref="T:System.IO.IOException">An I/O error occurs. </exception><exception cref="T:System.NotSupportedException">The stream does not support reading. </exception><exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
 public override int Read(byte[] buffer, int offset, int count)
 {
     try
     {
         LoadBlock(false);
         if (count == 0)
         {
             return(0);
         }
         int startOffset = (int)(_pos - _blockOffset);
         if (startOffset + count < _blockLength)
         {
             if (count == 1)
             {
                 buffer[offset] = _block[startOffset];
                 _pos++;
             }
             else
             {
                 Buffer.BlockCopy(_block, startOffset, buffer, offset, count);
                 _pos += count;
             }
             return(count);
         }
         if (_blockProvider.IsActive(_path, _blockOffset))
         {
             var availableByteCount = _blockLength - startOffset;
             if (availableByteCount < 0)
             {
                 Logging.LogError(BrightstarEventId.BlockProviderError,
                                  "Read(buff, {0}, {1}) about to fail. Block provider reports block with offset {2} as active block and internal length is {3}. AvailableByteCount={4}",
                                  offset, count, _blockOffset, _blockLength, availableByteCount);
             }
             Buffer.BlockCopy(_block, startOffset, buffer, offset, availableByteCount);
             _pos += availableByteCount;
             return(availableByteCount);
         }
         var bytesInThisBlock = _blockLength - startOffset;
         Buffer.BlockCopy(_block, startOffset, buffer, offset, bytesInThisBlock);
         _pos += bytesInThisBlock;
         return(bytesInThisBlock + Read(buffer, offset + bytesInThisBlock, count - bytesInThisBlock));
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarEventId.BlockProviderError,
                          "Error in BlockProviderStream.Read. Path is {0}, _pos={1}, _blockOffset={2}, _blockLength={3}. Read called with count={4}. Exception: {5}",
                          _path, _pos, _blockOffset, _blockLength, count, ex);
         throw;
     }
 }
 /// <summary>
 /// Determines if the block at the specified byte offset is the current active block
 /// </summary>
 /// <param name="path">The path to the block store to check</param>
 /// <param name="offset">The offset to check</param>
 /// <returns>True if the specified block is the current active block (the last bloock in the file), false otherwise.</returns>
 public bool IsActive(string path, long offset)
 {
     return(_blockProvider.IsActive(path, offset));
 }