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
파일 보기 프로젝트 열기: dotnet/corefx 1 사용 예제들

공개 메소드들

메소드 설명
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