Esempio n. 1
0
        /// <summary>
        /// Writes data to the stream at the current location.
        /// </summary>
        /// <param name="buffer">The data to write</param>
        /// <param name="offset">The first byte to write from buffer</param>
        /// <param name="count">The number of bytes to write</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            CheckDisposed();

            _stats.TotalWritesIn++;

            int  blockSize  = _settings.BlockSize;
            long firstBlock = _position / blockSize;
            long endBlock   = Utilities.Ceil(Math.Min(_position + count, Length), blockSize);
            int  numBlocks  = (int)(endBlock - firstBlock);

            try
            {
                _wrappedStream.Position = _position;
                _wrappedStream.Write(buffer, offset, count);
            }
            catch
            {
                InvalidateBlocks(firstBlock, numBlocks);
                throw;
            }

            int offsetInNextBlock = (int)(_position % blockSize);

            if (offsetInNextBlock != 0)
            {
                _stats.UnalignedWritesIn++;
            }

            // For each block touched, if it's cached, update it
            int bytesProcessed = 0;

            for (int i = 0; i < numBlocks; ++i)
            {
                int bufferPos      = offset + bytesProcessed;
                int bytesThisBlock = Math.Min(count - bytesProcessed, blockSize - offsetInNextBlock);

                Block block;
                if (_cache.TryGetBlock(firstBlock + i, out block))
                {
                    Array.Copy(buffer, bufferPos, block.Data, offsetInNextBlock, bytesThisBlock);
                    block.Available = Math.Max(block.Available, offsetInNextBlock + bytesThisBlock);
                }

                offsetInNextBlock = 0;
                bytesProcessed   += bytesThisBlock;
            }

            _position += count;
        }
Esempio n. 2
0
        /// <summary>
        /// Writes data to the stream (not currently supported).
        /// </summary>
        /// <param name="buffer">The data to write</param>
        /// <param name="offset">The first byte to write</param>
        /// <param name="count">The number of bytes to write</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            lock (_common)
            {
                SparseStream wrapped = Wrapped;

                if (_position + count > wrapped.Length)
                {
                    throw new IOException("Attempt to extend stream");
                }

                wrapped.Position = _position;
                wrapped.Write(buffer, offset, count);
                _position += count;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Writes a byte array into the buffer.
 /// </summary>
 /// <param name="pos">The start offset within the buffer.</param>
 /// <param name="buffer">The source byte array.</param>
 /// <param name="offset">The start offset within the source byte array.</param>
 /// <param name="count">The number of bytes to write.</param>
 public override void Write(long pos, byte[] buffer, int offset, int count)
 {
     _stream.Position = pos;
     _stream.Write(buffer, offset, count);
 }