C# Class System.IO.BufferedStream

One of the design goals here is to prevent the buffer from getting in the way and slowing down underlying stream accesses when it is not needed. If you always read & write for sizes greater than the internal buffer size, then this class may not even allocate the internal buffer. See a large comment in Write for the details of the write buffer heuristic. This class buffers reads & writes in a shared buffer. (If you maintained two buffers separately, one operation would always trash the other buffer anyways, so we might as well use one buffer.) The assumption here is you will almost always be doing a series of reads or writes, but rarely alternate between the two of them on the same stream. Class Invariants: The class has one buffer, shared for reading & writing. It can only be used for one or the other at any point in time - not both. The following should be true: 0 implies the read buffer is valid, but we're at the end of the buffer. * _readPos == _readLen == 0 means the read buffer contains garbage. * Either _writePos can be greater than 0, or _readLen & _readPos can be greater than zero, but neither can be greater than zero at the same time. ]]> This class will never cache more bytes than the max specified buffer size. However, it may use a temporary buffer of up to twice the size in order to combine several IO operations on the underlying stream into a single operation. This is because we assume that memory copies are significantly faster than IO operations on the underlying stream (if this was not true, using buffering is never appropriate). The max size of this "shadow" buffer is limited as to not allocate it on the LOH. Shadowing is always transient. Even when using this technique, this class still guarantees that the number of bytes cached (not yet written to the target stream or not yet consumed by the user) is never larger than the actual specified buffer size.
Inheritance: Stream
显示文件 Open project: dotnet/corefx Class Usage Examples

Public Methods

Method Description
BeginRead ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
BeginWrite ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
BufferedStream ( Stream stream ) : System.Runtime.InteropServices
BufferedStream ( Stream stream, int bufferSize ) : System.Runtime.InteropServices
CopyToAsync ( Stream destination, int bufferSize, CancellationToken cancellationToken ) : Task
EndRead ( IAsyncResult asyncResult ) : int
EndWrite ( IAsyncResult asyncResult ) : void
Flush ( ) : void
FlushAsync ( CancellationToken cancellationToken ) : Task
Read ( byte array, int offset, int count ) : int
ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
ReadByte ( ) : int
Seek ( long offset, SeekOrigin origin ) : long
SetLength ( long value ) : void
Write ( byte array, int offset, int count ) : void
WriteAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
WriteByte ( byte value ) : void

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Private Methods

Method Description
ClearReadBufferBeforeWrite ( ) : void

Called by Write methods to clear the Read Buffer

CopyToAsyncCore ( Stream destination, int bufferSize, CancellationToken cancellationToken ) : Task
EnsureBufferAllocated ( ) : void
EnsureCanRead ( ) : void
EnsureCanSeek ( ) : void
EnsureCanWrite ( ) : void
EnsureNotClosed ( ) : void
EnsureShadowBufferAllocated ( ) : void
FlushAsyncInternal ( CancellationToken cancellationToken ) : Task
FlushRead ( ) : void
FlushWrite ( ) : void
FlushWriteAsync ( CancellationToken cancellationToken ) : Task
LastSyncCompletedReadTask ( int val ) : Task
LazyEnsureAsyncActiveSemaphoreInitialized ( ) : SemaphoreSlim
ReadByteSlow ( ) : int
ReadFromBuffer ( Byte array, int offset, int count, Exception &error ) : int
ReadFromBuffer ( byte array, int offset, int count ) : int
ReadFromUnderlyingStreamAsync ( byte array, int offset, int count, CancellationToken cancellationToken, int bytesAlreadySatisfied, Task semaphoreLockTask ) : Task

BufferedStream should be as thin a wrapper as possible. We want ReadAsync to delegate to ReadAsync of the underlying _stream rather than calling the base Stream which implements the one in terms of the other. This allows BufferedStream to affect the semantics of the stream it wraps as little as possible.

WriteToBuffer ( byte array, int &offset, int &count ) : void
WriteToBuffer ( byte array, int &offset, int &count, Exception &error ) : void
WriteToUnderlyingStreamAsync ( byte array, int offset, int count, CancellationToken cancellationToken, Task semaphoreLockTask ) : Task

BufferedStream should be as thin a wrapper as possible. We want WriteAsync to delegate to WriteAsync of the underlying _stream rather than calling the base Stream which implements the one in terms of the other. This allows BufferedStream to affect the semantics of the stream it wraps as little as possible.

Method Details

BeginRead() public method

public BeginRead ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
count int
callback AsyncCallback
state object
return IAsyncResult

BeginWrite() public method

public BeginWrite ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
count int
callback AsyncCallback
state object
return IAsyncResult

BufferedStream() public method

public BufferedStream ( Stream stream ) : System.Runtime.InteropServices
stream Stream
return System.Runtime.InteropServices

BufferedStream() public method

public BufferedStream ( Stream stream, int bufferSize ) : System.Runtime.InteropServices
stream Stream
bufferSize int
return System.Runtime.InteropServices

CopyToAsync() public method

public CopyToAsync ( Stream destination, int bufferSize, CancellationToken cancellationToken ) : Task
destination Stream
bufferSize int
cancellationToken System.Threading.CancellationToken
return Task

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void

EndRead() public method

public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
return int

EndWrite() public method

public EndWrite ( IAsyncResult asyncResult ) : void
asyncResult IAsyncResult
return void

Flush() public method

public Flush ( ) : void
return void

FlushAsync() public method

public FlushAsync ( CancellationToken cancellationToken ) : Task
cancellationToken CancellationToken
return Task

Read() public method

public Read ( byte array, int offset, int count ) : int
array byte
offset int
count int
return int

ReadAsync() public method

public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
return Task

ReadByte() public method

public ReadByte ( ) : int
return int

Seek() public method

public Seek ( long offset, SeekOrigin origin ) : long
offset long
origin SeekOrigin
return long

SetLength() public method

public SetLength ( long value ) : void
value long
return void

Write() public method

public Write ( byte array, int offset, int count ) : void
array byte
offset int
count int
return void

WriteAsync() public method

public WriteAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
return Task

WriteByte() public method

public WriteByte ( byte value ) : void
value byte
return void