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
Afficher le fichier Open project: dotnet/corefx Class Usage Examples

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode Description
Dispose ( bool disposing ) : void

Private Methods

Méthode 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 méthode

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

BeginWrite() public méthode

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

BufferedStream() public méthode

public BufferedStream ( Stream stream ) : System.Runtime.InteropServices
stream Stream
Résultat System.Runtime.InteropServices

BufferedStream() public méthode

public BufferedStream ( Stream stream, int bufferSize ) : System.Runtime.InteropServices
stream Stream
bufferSize int
Résultat System.Runtime.InteropServices

CopyToAsync() public méthode

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

Dispose() protected méthode

protected Dispose ( bool disposing ) : void
disposing bool
Résultat void

EndRead() public méthode

public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
Résultat int

EndWrite() public méthode

public EndWrite ( IAsyncResult asyncResult ) : void
asyncResult IAsyncResult
Résultat void

Flush() public méthode

public Flush ( ) : void
Résultat void

FlushAsync() public méthode

public FlushAsync ( CancellationToken cancellationToken ) : Task
cancellationToken CancellationToken
Résultat Task

Read() public méthode

public Read ( byte array, int offset, int count ) : int
array byte
offset int
count int
Résultat int

ReadAsync() public méthode

public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
Résultat Task

ReadByte() public méthode

public ReadByte ( ) : int
Résultat int

Seek() public méthode

public Seek ( long offset, SeekOrigin origin ) : long
offset long
origin SeekOrigin
Résultat long

SetLength() public méthode

public SetLength ( long value ) : void
value long
Résultat void

Write() public méthode

public Write ( byte array, int offset, int count ) : void
array byte
offset int
count int
Résultat void

WriteAsync() public méthode

public WriteAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
Résultat Task

WriteByte() public méthode

public WriteByte ( byte value ) : void
value byte
Résultat void