Redzen.IO.MemoryBlockStream.Write C# (CSharp) Method

Write() public method

Writes a block of bytes into the stream.
public Write ( byte buffer, int offset, int count ) : void
buffer byte The byte data to write into the stream
offset int The zero-based byte offset in buffer from which to begin copying bytes to the current stream.
count int The maximum number of bytes to write.
return void
        public override void Write(byte[] buffer, int offset, int count)
        {
            // Basic checks.
            if(null == buffer) throw new ArgumentNullException("buffer", "Buffer cannot be null.");
            if(offset < 0) throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
            if(count < 0) throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
            if((buffer.Length - offset) < count) {
                throw new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
            }
            if(!this._isOpen) throw new ObjectDisposedException("Stream is closed.");

            if(0 == count)
            {   // Nothing to do.
                return;
            }

            // Determine new position (post write).
            int endPos = _position + count;
            // Check for overflow
            if(endPos < 0) throw new IOException("Stream was too long.");

            // Ensure there are enough blocks ready to write all of the provided data into.
            EnsureCapacity(endPos);

            // Write the bytes into the stream.
            int blockIdx = _position / _blockSize;
            int blockOffset = _position % _blockSize;
            WriteInner(buffer, offset, count, blockIdx, blockOffset);
        }

Usage Example

Exemplo n.º 1
0
        public void TestWriteZeroBytes()
        {
            byte[] buf = new byte[0];
            MemoryBlockStream ms = new MemoryBlockStream();
            ms.Write(buf, 0, 0);
            Assert.AreEqual(ms.Length, 0);

            XorShiftRandom rng = new XorShiftRandom(1234567);
            byte[] buf2 = new byte[100];
            rng.NextBytes(buf2);
            ms.Write(buf2, 0, buf2.Length);

            if(!Utils.AreEqual(ms.ToArray(), buf2)) Assert.Fail();

            ms.Write(buf, 0, 0);
            Assert.AreEqual(ms.Length, buf2.Length);
        }