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.​UnmanagedMemoryStream
Assembly: System.Runtime
Inheritance: object → MarshalByRefObject → Stream → UnmanagedMemoryStream
Implemented Interfaces
- IAsyncDisposable Implemented by: Stream
- IDisposable Implemented by: Stream
Provides access to unmanaged blocks of memory from managed code.
Properties
public
bool
CanRead
Gets a value indicating whether a stream supports reading.
public
bool
CanSeek
Gets a value indicating whether a stream supports seeking.
public
bool
CanWrite
Gets a value indicating whether a stream supports writing.
public
long
Capacity
Gets the stream length (size) or the total amount of memory assigned to a stream (capacity).
public
long
Length
Gets the length of the data in a stream.
public
long
Position
Gets or sets the current position in a stream.
public
Byte*
PositionPointer
Gets or sets a byte pointer to a stream based on the current position in the stream.
public
bool
CanTimeout
Gets a value that determines whether the current stream can time out.
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
protected
void
Dispose​(bool disposing)
Releases the unmanaged resources used by the <see cref="T:System.IO.UnmanagedMemoryStream" /> and optionally releases the managed resources.
disposing
<see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.
public
void
Flush​()
Overrides the <see cref="M:System.IO.Stream.Flush" /> method so that no action is performed.
FlushAsync​(Threading.​CancellationToken cancellationToken)
Overrides the <see cref="M:System.IO.Stream.FlushAsync(System.Threading.CancellationToken)" /> method so that the operation is cancelled if specified, but no other action is performed.
Returns A task that represents the asynchronous flush operation.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
protected
void
Initialize​(Byte* pointer,
long length,
long capacity,
FileAccess access)
Initializes a new instance of the <see cref="T:System.IO.UnmanagedMemoryStream" /> class by using a pointer to an unmanaged memory location.
pointer
A pointer to an unmanaged memory location.
length
The length of the memory to use.
capacity
The total amount of memory assigned to the stream.
access
One of the <see cref="T:System.IO.FileAccess" /> values.
protected
void
Initialize​(Runtime.​InteropServices.​SafeBuffer buffer,
long offset,
long length,
FileAccess access)
Initializes a new instance of the <see cref="T:System.IO.UnmanagedMemoryStream" /> class in a safe buffer with a specified offset, length, and file access.
buffer
The buffer to contain the unmanaged memory stream.
offset
The byte position in the buffer at which to start the unmanaged memory stream.
length
The length of the unmanaged memory stream.
access
The mode of file access to the unmanaged memory stream.
public
int
Read​(byte[] buffer,
int offset,
int count)
Reads the specified number of bytes into the specified array.
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 the end of the stream has been reached.
buffer
When this method returns, 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. This parameter is passed uninitialized.
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 read from the current stream.
public
int
Read​(Span<​byte> buffer)
Reads all the bytes of this unmanaged memory stream into the specified span of bytes.
Returns The total number of bytes read into the destination.
public
Threading.​Tasks.​Task<​int>
ReadAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken)
Asynchronously reads the specified number of bytes into the specified array.
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 the end of the stream has been reached.
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" /> .
public
Threading.​Tasks.​ValueTask<​int>
ReadAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Asynchronously reads the unmanaged memory stream bytes into the memory region.
Returns A task that represents the asynchronous read operation, and wraps the total number of bytes read into the buffer.
buffer
When the asynchronous method finishes, this memory region contains all the bytes read from the unmanaged memory stream.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
public
int
ReadByte​()
Reads a byte from a stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
Returns The unsigned byte cast to an <see cref="T:System.Int32" /> object, or -1 if at the end of the stream.
public
long
Seek​(long offset,
SeekOrigin loc)
Sets the current position of the current stream to the given value.
Returns The new position in the stream.
offset
The point relative to <c>origin</c> to begin seeking from.
loc
Specifies the beginning, the end, or the current position as a reference point for <c>origin</c> , using a value of type <see cref="T:System.IO.SeekOrigin" /> .
public
void
SetLength​(long value)
Sets the length of a stream to a specified value.
value
The length of the stream.
public
void
Write​(byte[] buffer,
int offset,
int count)
Writes a block of bytes to the current stream using data from a buffer.
buffer
The byte array from which to copy bytes to the current stream.
offset
The offset in the buffer at which to begin copying bytes to the current stream.
count
The number of bytes to write to the current stream.
public
void
Write​(ReadOnlySpan<​byte> buffer)
Writes a block of bytes to the current unmanaged memory stream using data from the provided span of bytes.
WriteAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken)
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.
Returns A task that represents the asynchronous write operation.
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" /> .
WriteAsync​(ReadOnlyMemory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Asynchronously writes a span of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
Returns A task that represents the asynchronous write operation.
buffer
The buffer to write data from.
cancellationToken
The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" /> .
public
void
WriteByte​(byte value)
Writes a byte to the current position in the file stream.
value
A byte value written to the stream.
public
IAsyncResult
BeginRead​(byte[] buffer,
int offset,
int count,
AsyncCallback? callback,
object state)
Inherited from Stream
Begins an asynchronous read operation. (Consider using <see cref="M:System.IO.Stream.ReadAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
Returns An <see cref="T:System.IAsyncResult" /> that represents the asynchronous read, which could still be pending.
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.
public
IAsyncResult
BeginWrite​(byte[] buffer,
int offset,
int count,
AsyncCallback? callback,
object state)
Inherited from Stream
Begins an asynchronous write operation. (Consider using <see cref="M:System.IO.Stream.WriteAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
Returns An <see langword="IAsyncResult" /> that represents the asynchronous write, which could still be pending.
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.
public
void
Close​()
Inherited from Stream
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)
Inherited from Stream
Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
destination
The stream to which the contents of the current stream will be copied.
public
void
CopyTo​(Stream destination,
int bufferSize)
Inherited from Stream
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.
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.
CopyToAsync​(Stream destination)
Inherited from Stream
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.
Returns A task that represents the asynchronous copy operation.
destination
The stream to which the contents of the current stream will be copied.
CopyToAsync​(Stream destination,
int bufferSize)
Inherited from Stream
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.
Returns A task that represents the asynchronous copy operation.
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.
CopyToAsync​(Stream destination,
int bufferSize,
Threading.​CancellationToken cancellationToken)
Inherited from Stream
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.
Returns A task that represents the asynchronous copy operation.
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" /> .
CopyToAsync​(Stream destination,
Threading.​CancellationToken cancellationToken)
Inherited from Stream
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.
Returns A task that represents the asynchronous copy operation.
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" /> .
protected
Threading.​WaitHandle
CreateWaitHandle​()
Inherited from Stream
Allocates a <see cref="T:System.Threading.WaitHandle" /> object.
Returns A reference to the allocated <see langword="WaitHandle" /> .
public
void
Dispose​()
Inherited from Stream
Releases all resources used by the <see cref="T:System.IO.Stream" /> .
DisposeAsync​()
Inherited from Stream
Asynchronously releases the unmanaged resources used by the <see cref="T:System.IO.Stream" /> .
Returns A task that represents the asynchronous dispose operation.
public
int
EndRead​(IAsyncResult asyncResult)
Inherited from Stream
Waits for the pending asynchronous read to complete. (Consider using <see cref="M:System.IO.Stream.ReadAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
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).
asyncResult
The reference to the pending asynchronous request to finish.
public
void
EndWrite​(IAsyncResult asyncResult)
Inherited from Stream
Ends an asynchronous write operation. (Consider using <see cref="M:System.IO.Stream.WriteAsync(System.Byte[],System.Int32,System.Int32)" /> instead.)
asyncResult
A reference to the outstanding asynchronous I/O request.
FlushAsync​()
Inherited from Stream
Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.
Returns A task that represents the asynchronous flush operation.
protected
void
ObjectInvariant​()
Inherited from Stream
Provides support for a <see cref="T:System.Diagnostics.Contracts.Contract" /> .
public
Threading.​Tasks.​Task<​int>
ReadAsync​(byte[] buffer,
int offset,
int count)
Inherited from Stream
Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes 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.
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.
public
int
ReadAtLeast​(Span<​byte> buffer,
int minimumBytes,
bool throwOnEndOfStream = true)
Inherited from Stream
Reads at least a minimum number of bytes from the current stream and advances the position within the stream by the number of bytes read.
Returns The total number of bytes read into the buffer. This is guaranteed to be greater than or equal to <paramref name="minimumBytes" /> when <paramref name="throwOnEndOfStream" /> is <see langword="true" /> . This will be less than <paramref name="minimumBytes" /> when the end of the stream is reached and <paramref name="throwOnEndOfStream" /> is <see langword="false" /> . This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available.
buffer
A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.
minimumBytes
The minimum number of bytes to read into the buffer.
throwOnEndOfStream
<see langword="true" /> to throw an exception if the end of the stream is reached before reading <paramref name="minimumBytes" /> of bytes; <see langword="false" /> to return less than <paramref name="minimumBytes" /> when the end of the stream is reached.
The default is <see langword="true" /> .
public
Threading.​Tasks.​ValueTask<​int>
ReadAtLeastAsync​(Memory<​byte> buffer,
int minimumBytes,
bool throwOnEndOfStream = true,
Threading.​CancellationToken cancellationToken = null)
Inherited from Stream
Asynchronously reads at least a minimum number of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
Returns A task that represents the asynchronous read operation. The value of its <see cref="P:System.Threading.Tasks.ValueTask`1.Result" /> property contains the total number of bytes read into the buffer. This is guaranteed to be greater than or equal to <paramref name="minimumBytes" /> when <paramref name="throwOnEndOfStream" /> is <see langword="true" /> . This will be less than <paramref name="minimumBytes" /> when the end of the stream is reached and <paramref name="throwOnEndOfStream" /> is <see langword="false" /> . This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available.
buffer
The region of memory to write the data into.
minimumBytes
The minimum number of bytes to read into the buffer.
throwOnEndOfStream
<see langword="true" /> to throw an exception if the end of the stream is reached before reading <paramref name="minimumBytes" /> of bytes; <see langword="false" /> to return less than <paramref name="minimumBytes" /> when the end of the stream is reached.
The default is <see langword="true" /> .
cancellationToken
The token to monitor for cancellation requests.
public
void
ReadExactly​(byte[] buffer,
int offset,
int count)
Inherited from Stream
Reads <paramref name="count" /> number of bytes from the current stream and advances the position within the stream.
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.
public
void
ReadExactly​(Span<​byte> buffer)
Inherited from Stream
Reads bytes from the current stream and advances the position within the stream until the <paramref name="buffer" /> is filled.
buffer
A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.
ReadExactlyAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken = null)
Inherited from Stream
Asynchronously reads <paramref name="count" /> number of bytes from the current stream, advances the position within the stream, and monitors cancellation requests.
Returns A task that represents the asynchronous read operation.
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.
ReadExactlyAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Inherited from Stream
Asynchronously reads bytes from the current stream, advances the position within the stream until the <paramref name="buffer" /> is filled, and monitors cancellation requests.
Returns A task that represents the asynchronous read operation.
buffer
The buffer to write the data into.
cancellationToken
The token to monitor for cancellation requests.
WriteAsync​(byte[] buffer,
int offset,
int count)
Inherited from Stream
Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
Returns A task that represents the asynchronous write operation.
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.
public
object
GetLifetimeService​()
Inherited from MarshalByRefObject
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
Returns An object of type <see cref="T:System.Runtime.Remoting.Lifetime.ILease" /> used to control the lifetime policy for this instance.
public
object
InitializeLifetimeService​()
Inherited from MarshalByRefObject
Obtains a lifetime service object to control the lifetime policy for this instance.
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.
protected
MarshalByRefObject
MemberwiseClone​(bool cloneIdentity)
Inherited from MarshalByRefObject
Creates a shallow copy of the current <see cref="T:System.MarshalByRefObject" /> object.
Returns A shallow copy of the current <see cref="T:System.MarshalByRefObject" /> object.
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.
public
bool
Equals​(object obj)
Inherited from object
Determines whether the specified object is equal to the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
obj
The object to compare with 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
Serves as the default hash function.
Returns A hash code for the current object.
public
Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected
object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns 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.