C# I/O flow: Stream

Stream The abstract base class for all streams.

Data stream (Stream) is an abstract representation of serial transmission data. It is an abstract data input and output with source and destination. The connection between the two is the stream object.


A stream is a conduit for data to and from

Reading is transferring data from a stream (pipeline) to a data structure
Writing is transferring data from a data structure to a stream (pipeline)


The specific properties and methods will be written in the subclass of Stream

The content is excerpted from: https://learn.microsoft.com/zh-cn/dotnet/api/system.io.stream?view=net-7.0

1. Field

Null     
A Stream with no backing store.

2. Properties

Position     
When overridden in a derived class, gets or sets the position in the current stream.

Length     
When overridden in a derived class, gets the stream length in bytes.

CanWrite     
When overridden in a derived class, gets a value indicating whether the current stream supports write functionality.

CanTimeout     
gets a value that determines whether the current stream can time out.

CanSeek     
When overridden in a derived class, gets a value indicating whether the current stream supports seek functionality.

CanRead     
When overridden in a derived class, gets a value indicating whether the current stream supports reading.

ReadTimeout     
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.

WriteTimeout     
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.

Three. Method

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)     
starts an asynchronous read operation. (Consider using ReadAsync(Byte[], Int32, Int32) instead.)

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)     
starts an asynchronous write operation. (Consider using WriteAsync(Byte[], Int32, Int32) instead.)

Close()     
closes the current stream and releases all resources associated with it (such as sockets and file handles). Do not call this method directly, but ensure that the stream is properly released.

CopyTo(Stream)     
reads bytes from the current stream and writes them to another stream.

CopyTo(Stream, Int32)     
reads bytes from the current stream and writes them to another stream, using the specified buffer size.

CopyToAsync(Stream)     
asynchronously reads bytes from the current stream and writes them to another stream.

CopyToAsync(Stream, CancellationToken)     
asynchronously reads bytes from the current stream and writes them to another stream with the specified cancellation token.

CopyToAsync(Stream, Int32)     
asynchronously reads bytes from the current stream and writes them to another stream, using the specified buffer size.

CopyToAsync(Stream, Int32, CancellationToken)     
asynchronously reads bytes from the current stream and writes them to another stream, using the specified buffer size and cancellation token.

Dispose()     
releases all resources used by the Stream.

EndRead(IAsyncResult)     
waits for a pending asynchronous read to complete. (Consider using ReadAsync(Byte[], Int32, Int32) instead.)

EndWrite(IAsyncResult)     
ends the asynchronous write operation. (Consider using WriteAsync(Byte[], Int32, Int32) instead.)

Flush(),     
when overridden in a derived class, clears all buffers for this stream and causes all buffered data to be written to the underlying device.

FlushAsync()     
asynchronously flushes all buffers for this stream and causes all buffered data to be written to the underlying device.

FlushAsync(CancellationToken)     
flushes all buffers for this stream asynchronously, causing all buffered data to be written to the underlying device, and monitors for cancellation requests.

Read(Byte[], Int32, Int32)     
When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position in this stream by the number of bytes read.

ReadAsync(Byte[], Int32, Int32)     
asynchronously reads a sequence of bytes from the current stream and advances the position in the stream by the number of bytes read.

ReadAsync(Byte[], Int32, Int32, CancellationToken)     
asynchronously reads a sequence of bytes from the current stream, advances the position in the stream by the number of bytes read, and monitors for cancellation requests.

ReadByte()     
reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if the end of the stream has been reached.

Seek(Int64, SeekOrigin)     
When overridden in a derived class, sets the position in the current stream.

SetLength(Int64)     
When overridden in a derived class, sets the length of the current stream.

Write(Byte[], Int32, Int32)     
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position in this stream by the number of bytes written.

WriteAsync(Byte[], Int32, Int32)     
asynchronously writes a sequence of bytes to the current stream and advances the current position of the stream by the number of bytes written.

WriteAsync(Byte[], Int32, Int32, CancellationToken)     
asynchronously writes a sequence of bytes to the current stream, advances the current position in that stream by the number of bytes written, and monitors for cancellation requests.

WriteByte(Byte)     
writes a byte to the current position within the stream and advances the position within the stream by one byte.

Dispose(Boolean)     
Releases unmanaged resources occupied by the Stream, and may additionally release managed resources.

Guess you like

Origin blog.csdn.net/SmillCool/article/details/127782654