Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​IO.​Stream
Assembly: System.Runtime
Inheritance: object → MarshalByRefObject → Stream
Provides a generic view of a sequence of bytes. This is an abstract class.
Fields and Constants
public static readonly
Stream
Null
A <see langword="Stream" /> with no backing store.
Properties
public
bool
CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports reading.
public
bool
CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
public
bool
CanTimeout
Gets a value that determines whether the current stream can time out.
public
bool
CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writing.
public
long
Length
When overridden in a derived class, gets the length in bytes of the stream.
public
long
Position
When overridden in a derived class, gets or sets the position within the current stream.
public
int
ReadTimeout
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.
public
int
WriteTimeout
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.
Methods
public
IAsyncResult
BeginRead​(byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state)
buffer
The buffer to read the data into.
offset
The byte offset in <paramref name="buffer" /> at which to begin writing data read from the stream.
count
The maximum number of bytes to read.
callback
An optional asynchronous callback, to be called when the read is complete.
state
A user-provided object that distinguishes this particular asynchronous read request from other requests.
Returns An <see cref="T:System.IAsyncResult" /> that represents the asynchronous read, which could still be pending.
Begins an asynchronous read operation. (Consider using <see cref="M:System.IO.Stream.ReadAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
public
IAsyncResult
BeginWrite​(byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state)
buffer
The buffer to write data from.
offset
The byte offset in <paramref name="buffer" /> from which to begin writing.
count
The maximum number of bytes to write.
callback
An optional asynchronous callback, to be called when the write is complete.
state
A user-provided object that distinguishes this particular asynchronous write request from other requests.
Returns An <see langword="IAsyncResult" /> that represents the asynchronous write, which could still be pending.
Begins an asynchronous write operation. (Consider using <see cref="M:System.IO.Stream.WriteAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
public
void
Close​()
Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
public
void
CopyTo​(Stream destination)
destination
The stream to which the contents of the current stream will be copied.
Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
public
void
CopyTo​(Stream destination,
int bufferSize)
destination
The stream to which the contents of the current stream will be copied.
bufferSize
The size of the buffer. This value must be greater than zero. The default size is 81920.
Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Both streams positions are advanced by the number of bytes copied.
CopyToAsync​(Stream destination)
destination
The stream to which the contents of the current stream will be copied.
Returns A task that represents the asynchronous copy operation.
Asynchronously reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
CopyToAsync​(Stream destination,
int bufferSize)
destination
The stream to which the contents of the current stream will be copied.
bufferSize
The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.
Returns A task that represents the asynchronous copy operation.
Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Both streams positions are advanced by the number of bytes copied.
CopyToAsync​(Stream destination,
int bufferSize,
Threading.CancellationToken cancellationToken)
destination
The stream to which the contents of the current stream will be copied.
bufferSize
The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
Returns A task that represents the asynchronous copy operation.
Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token. Both streams positions are advanced by the number of bytes copied.
CopyToAsync​(Stream destination,
Threading.CancellationToken cancellationToken)
destination
The stream to which the contents of the current stream will be copied.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
Returns A task that represents the asynchronous copy operation.
Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token. Both streams positions are advanced by the number of bytes copied.
protected
Threading.​WaitHandle
CreateWaitHandle​() Returns A reference to the allocated <see langword="WaitHandle" /> .
Allocates a <see cref="T:System.Threading.WaitHandle" /> object.
public
void
Dispose​()
Releases all resources used by the <see cref="T:System.IO.Stream" /> .
protected
void
Dispose​(bool disposing)
disposing
<see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.
Releases the unmanaged resources used by the <see cref="T:System.IO.Stream" /> and optionally releases the managed resources.
DisposeAsync​()
Returns A task that represents the asynchronous dispose operation.
Asynchronously releases the unmanaged resources used by the <see cref="T:System.IO.Stream" /> .
public
int
EndRead​(IAsyncResult asyncResult)
asyncResult
The reference to the pending asynchronous request to finish.
Returns The number of bytes read from the stream, between zero (0) and the number of bytes requested. ReadAsync returns zero (0) only if zero bytes were requested or if no more bytes will be available because it's at the end of the stream; otherwise, read operations do not complete until at least one byte is available. If zero bytes are requested, read operations may complete immediately or may not complete until at least one byte is available (but without consuming any data).
Waits for the pending asynchronous read to complete. (Consider using <see cref="M:System.IO.Stream.ReadAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
public
void
EndWrite​(IAsyncResult asyncResult)
asyncResult
A reference to the outstanding asynchronous I/O request.
Ends an asynchronous write operation. (Consider using <see cref="M:System.IO.Stream.WriteAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
public
void
Flush​()
When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
FlushAsync​()
Returns A task that represents the asynchronous flush operation.
Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.
FlushAsync​(Threading.CancellationToken cancellationToken)
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
Returns A task that represents the asynchronous flush operation.
Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.
protected
void
ObjectInvariant​()
Provides support for a <see cref="T:System.Diagnostics.Contracts.Contract" /> .
public
int
Read​(byte[] buffer,
int offset,
int count)
buffer
An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset" /> and ( <paramref name="offset" /> + <paramref name="count" /> - 1) replaced by the bytes read from the current source.
offset
The zero-based byte offset in <paramref name="buffer" /> at which to begin storing the data read from the current stream.
count
The maximum number of bytes to be read from the current stream.
Returns The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if <paramref name="count" /> is 0 or the end of the stream has been reached.
When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
ReadAsync​(byte[] buffer,
int offset,
int count)
buffer
The buffer to write the data into.
offset
The byte offset in <paramref name="buffer" /> at which to begin writing data from the stream.
count
The maximum number of bytes to read.
Returns A task that represents the asynchronous read operation. The value of the <paramref name="TResult" /> parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if <paramref name="count" /> is 0 or if the end of the stream has been reached.
Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
ReadAsync​(byte[] buffer,
int offset,
int count,
Threading.CancellationToken cancellationToken)
buffer
The buffer to write the data into.
offset
The byte offset in <paramref name="buffer" /> at which to begin writing data from the stream.
count
The maximum number of bytes to read.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
Returns A task that represents the asynchronous read operation. The value of the <paramref name="TResult" /> parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if <paramref name="count" /> is 0 or if the end of the stream has been reached.
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
public
int
ReadAtLeast​(Span`1 buffer,
int minimumBytes,
bool throwOnEndOfStream = True)
ReadAtLeastAsync​(Memory`1 buffer,
int minimumBytes,
bool throwOnEndOfStream = True,
Threading.CancellationToken cancellationToken = null)
public
int
ReadByte​() Returns The unsigned byte cast to an <see cref="T:System.Int32" /> , or -1 if at the end of the stream.
Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
public
void
ReadExactly​(byte[] buffer,
int offset,
int count)
buffer
An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset" /> and ( <paramref name="offset" /> + <paramref name="count" /> - 1) replaced by the bytes read from the current stream.
offset
The byte offset in <paramref name="buffer" /> at which to begin storing the data read from the current stream.
count
The number of bytes to be read from the current stream.
Reads <paramref name="count" /> number of bytes from the current stream and advances the position within the stream.
ReadExactlyAsync​(byte[] buffer,
int offset,
int count,
Threading.CancellationToken cancellationToken = null)
buffer
The buffer to write the data into.
offset
The byte offset in <paramref name="buffer" /> at which to begin writing data from the stream.
count
The number of bytes to be read from the current stream.
cancellationToken
The token to monitor for cancellation requests.
Returns A task that represents the asynchronous read operation.
Asynchronously reads <paramref name="count" /> number of bytes from the current stream, advances the position within the stream, and monitors cancellation requests.
ReadExactlyAsync​(Memory`1 buffer,
Threading.CancellationToken cancellationToken = null)
public
long
Seek​(long offset,
SeekOrigin origin)
offset
A byte offset relative to the <paramref name="origin" /> parameter.
origin
A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to obtain the new position.
Returns The new position within the current stream.
When overridden in a derived class, sets the position within the current stream.
public
void
SetLength​(long value)
value
The desired length of the current stream in bytes.
When overridden in a derived class, sets the length of the current stream.
public
void
Write​(byte[] buffer,
int offset,
int count)
buffer
An array of bytes. This method copies <paramref name="count" /> bytes from <paramref name="buffer" /> to the current stream.
offset
The zero-based byte offset in <paramref name="buffer" /> at which to begin copying bytes to the current stream.
count
The number of bytes to be written to the current stream.
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
WriteAsync​(byte[] buffer,
int offset,
int count)
buffer
The buffer to write data from.
offset
The zero-based byte offset in <paramref name="buffer" /> from which to begin copying bytes to the stream.
count
The maximum number of bytes to write.
Returns A task that represents the asynchronous write operation.
Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
WriteAsync​(byte[] buffer,
int offset,
int count,
Threading.CancellationToken cancellationToken)
buffer
The buffer to write data from.
offset
The zero-based byte offset in <paramref name="buffer" /> from which to begin copying bytes to the stream.
count
The maximum number of bytes to write.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
Returns A task that represents the asynchronous write operation.
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
WriteAsync​(ReadOnlyMemory`1 buffer,
Threading.CancellationToken cancellationToken = null)
public
void
WriteByte​(byte value)
value
The byte to write to the stream.
Writes a byte to the current position in the stream and advances the position within the stream by one byte.
public
object
GetLifetimeService​()
Inherited from MarshalByRefObject
Returns An object of type <see cref="T:System.Runtime.Remoting.Lifetime.ILease" /> used to control the lifetime policy for this instance.
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
public
object
InitializeLifetimeService​()
Inherited from MarshalByRefObject
Returns An object of type <see cref="T:System.Runtime.Remoting.Lifetime.ILease" /> used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the <see cref="P:System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseManagerPollTime" /> property.
Obtains a lifetime service object to control the lifetime policy for this instance.
protected
MarshalByRefObject
MemberwiseClone​(bool cloneIdentity)
Inherited from MarshalByRefObject
cloneIdentity
<see langword="false" /> to delete the current <see cref="T:System.MarshalByRefObject" /> object's identity, which will cause the object to be assigned a new identity when it is marshaled across a remoting boundary. A value of <see langword="false" /> is usually appropriate. <see langword="true" /> to copy the current <see cref="T:System.MarshalByRefObject" /> object's identity to its clone, which will cause remoting client calls to be routed to the remote server object.
Returns A shallow copy of the current <see cref="T:System.MarshalByRefObject" /> object.
Creates a shallow copy of the current <see cref="T:System.MarshalByRefObject" /> object.
public
bool
Equals​(object obj)
Inherited from object
obj
The object to compare with the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
Determines whether the specified object is equal to the current object.
protected
void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public
int
GetHashCode​()
Inherited from object
Returns A hash code for the current object.
Serves as the default hash function.
public
Type
GetType​()
Inherited from object
Returns The exact runtime type of the current instance.
Gets the <see cref="T:System.Type" /> of the current instance.
protected
object
MemberwiseClone​()
Inherited from object
Returns A shallow copy of the current <see cref="T:System.Object" /> .
Creates a shallow copy of the current <see cref="T:System.Object" /> .
public
string
ToString​()
Inherited from object
Returns A string that represents the current object.
Returns a string that represents the current object.