C# Класс 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.
Наследование: Stream
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
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

Защищенные методы

Метод Описание
Dispose ( bool disposing ) : void

Приватные методы

Метод Описание
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.

Описание методов

BeginRead() публичный Метод

public BeginRead ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
count int
callback AsyncCallback
state object
Результат IAsyncResult

BeginWrite() публичный Метод

public BeginWrite ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
count int
callback AsyncCallback
state object
Результат IAsyncResult

BufferedStream() публичный Метод

public BufferedStream ( Stream stream ) : System.Runtime.InteropServices
stream Stream
Результат System.Runtime.InteropServices

BufferedStream() публичный Метод

public BufferedStream ( Stream stream, int bufferSize ) : System.Runtime.InteropServices
stream Stream
bufferSize int
Результат System.Runtime.InteropServices

CopyToAsync() публичный Метод

public CopyToAsync ( Stream destination, int bufferSize, CancellationToken cancellationToken ) : Task
destination Stream
bufferSize int
cancellationToken System.Threading.CancellationToken
Результат Task

Dispose() защищенный Метод

protected Dispose ( bool disposing ) : void
disposing bool
Результат void

EndRead() публичный Метод

public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
Результат int

EndWrite() публичный Метод

public EndWrite ( IAsyncResult asyncResult ) : void
asyncResult IAsyncResult
Результат void

Flush() публичный Метод

public Flush ( ) : void
Результат void

FlushAsync() публичный Метод

public FlushAsync ( CancellationToken cancellationToken ) : Task
cancellationToken CancellationToken
Результат Task

Read() публичный Метод

public Read ( byte array, int offset, int count ) : int
array byte
offset int
count int
Результат int

ReadAsync() публичный Метод

public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
Результат Task

ReadByte() публичный Метод

public ReadByte ( ) : int
Результат int

Seek() публичный Метод

public Seek ( long offset, SeekOrigin origin ) : long
offset long
origin SeekOrigin
Результат long

SetLength() публичный Метод

public SetLength ( long value ) : void
value long
Результат void

Write() публичный Метод

public Write ( byte array, int offset, int count ) : void
array byte
offset int
count int
Результат void

WriteAsync() публичный Метод

public WriteAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
Результат Task

WriteByte() публичный Метод

public WriteByte ( byte value ) : void
value byte
Результат void