Write() public method

public Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void
示例#1
0
        private void DoOperation(ModifyStream modifyStream, ModifyBuffer modifyBuffer, int count)
        {
            int startOffset = (int)(_position % _blockSize);

            if (startOffset == 0 && (count % _blockSize == 0 || _position + count == Length))
            {
                WrappedStream.Position = _position;
                modifyStream(WrappedStream, 0, count);
                _position += count;
                return;
            }

            long unalignedEnd = _position + count;
            long alignedPos   = MathUtilities.RoundDown(_position, _blockSize);

            if (startOffset != 0)
            {
                WrappedStream.Position = alignedPos;
                WrappedStream.Read(_alignmentBuffer, 0, _blockSize);

                modifyBuffer(_alignmentBuffer, startOffset, 0, Math.Min(count, _blockSize - startOffset));

                WrappedStream.Position = alignedPos;
                WrappedStream.Write(_alignmentBuffer, 0, _blockSize);
            }

            alignedPos = MathUtilities.RoundUp(_position, _blockSize);
            if (alignedPos >= unalignedEnd)
            {
                _position = unalignedEnd;
                return;
            }

            int passthroughLength = (int)MathUtilities.RoundDown(_position + count - alignedPos, _blockSize);

            if (passthroughLength > 0)
            {
                WrappedStream.Position = alignedPos;
                modifyStream(WrappedStream, (int)(alignedPos - _position), passthroughLength);
            }

            alignedPos += passthroughLength;
            if (alignedPos >= unalignedEnd)
            {
                _position = unalignedEnd;
                return;
            }

            WrappedStream.Position = alignedPos;
            WrappedStream.Read(_alignmentBuffer, 0, _blockSize);

            modifyBuffer(_alignmentBuffer, 0, (int)(alignedPos - _position), (int)Math.Min(count - (alignedPos - _position), unalignedEnd - alignedPos));

            WrappedStream.Position = alignedPos;
            WrappedStream.Write(_alignmentBuffer, 0, _blockSize);

            _position = unalignedEnd;
        }
示例#2
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     if (m_Position + count > m_Offset + m_Size)
     {
         throw new NotSupportedException(SR.GetString(SR.net_cache_unsupported_partial_stream));
     }
     WrappedStream.Write(buffer, offset, count);
     m_Position += count;
 }
示例#3
0
        public async Task Parameterized_Methods_Are_Properly_Wrapped()
        {
            var strm = Substitute.For <Stream>();

            strm.CanRead.Returns(true);
            strm.CanWrite.Returns(true);
            using (var wrprstrm = new WrappedStream(strm, false))
            {
                wrprstrm.Seek(0, SeekOrigin.Current);
                strm.Received(1).Seek(0, SeekOrigin.Current);

                wrprstrm.SetLength(10);
                strm.Received(1).SetLength(10);

                var arrByte = new byte[5];
                wrprstrm.Read(arrByte, 0, arrByte.Length);
                strm.Received(1).Read(arrByte, 0, arrByte.Length);

                wrprstrm.Write(arrByte, 0, arrByte.Length);
                strm.Received(1).Write(arrByte, 0, arrByte.Length);

                var anotherStrm = new MemoryStream();
                await wrprstrm.CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                await wrprstrm.ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                wrprstrm.WriteByte(20);
                strm.Received(1).WriteByte(20);

                var _  = wrprstrm.Equals(null);
                var __ = strm.Received(1).Equals(null);
            }
        }
示例#4
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     WrappedStream.Write(buffer, offset, count);
 }
示例#5
0
 /// <summary>
 /// Writes a sequence of bytes to the current stream and advances the current position
 /// within this stream by the number of bytes written.
 /// </summary>
 public override void Write(byte[] buffer, int offset, int count)
 {
     ThrowIfDisposed();
     WrappedStream.Write(buffer, offset, count);
 }
        public async Task Parameterized_Methods_Are_Properly_Wrapped()
        {
            var strm = Substitute.For <Stream>();

            using (var wrprstrm = new WrappedStream(strm, false))
            {
                wrprstrm.Seek(0, SeekOrigin.Current);
                strm.Received(1).Seek(0, SeekOrigin.Current);

                wrprstrm.SetLength(10);
                strm.Received(1).SetLength(10);

                var arrByte = new byte[5];
                wrprstrm.Read(arrByte, 0, arrByte.Length);
                strm.Received(1).Read(arrByte, 0, arrByte.Length);

                wrprstrm.Write(arrByte, 0, arrByte.Length);
                strm.Received(1).Write(arrByte, 0, arrByte.Length);

                var anotherStrm = new MemoryStream();
                await wrprstrm.CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                await wrprstrm.ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                var cb     = new AsyncCallback(ar => { });
                var result = wrprstrm.BeginRead(arrByte, 0, arrByte.Length, cb, null);
                strm.Received(1).BeginRead(arrByte, 0, arrByte.Length, cb, null);

                wrprstrm.BeginWrite(arrByte, 0, arrByte.Length, cb, null);
                strm.Received(1).BeginWrite(arrByte, 0, arrByte.Length, cb, null);

#if FEATURE_REMOTING
                wrprstrm.CreateObjRef(null);
                strm.Received(1).CreateObjRef(null);

                wrprstrm.InitializeLifetimeService();
                strm.Received(1).InitializeLifetimeService();
#else
                Assert.True(Assert.Throws <RemotingException>(() => wrprstrm.CreateObjRef(null))
                            .Message.Equals(WrappedStream.RemotingErrorTxt));
                Assert.True(Assert.Throws <RemotingException>(() => wrprstrm.InitializeLifetimeService())
                            .Message.Equals(WrappedStream.RemotingErrorTxt));
#endif
                wrprstrm.EndRead(result);
                strm.Received(1).EndRead(result);

                wrprstrm.EndWrite(result);
                strm.Received(1).EndWrite(result);

                wrprstrm.WriteByte(20);
                strm.Received(1).WriteByte(20);

                wrprstrm.Equals(null);
                strm.Received(1).Equals(null);
            }
        }