Uredi

Deli z drugimi prek


MemoryStream.Read Method

Definition

Overloads

Read(Span<Byte>)

Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read.

Read(Byte[], Int32, Int32)

Reads a block of bytes from the current stream and writes the data to a buffer.

Read(Span<Byte>)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read.

public:
 override int Read(Span<System::Byte> destination);
public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> destination);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
override this.Read : Span<byte> -> int
Public Overrides Function Read (destination As Span(Of Byte)) As Integer
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

Parameters

destinationbuffer
Span<Byte>

A region of memory. When this method returns, the contents of this span are replaced by the bytes read from the current memory stream source.

Returns

The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the memory stream has been reached.

Applies to

Read(Byte[], Int32, Int32)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Reads a block of bytes from the current stream and writes the data to a buffer.

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

Parameters

buffer
Byte[]

When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.

offset
Int32

The zero-based byte offset in buffer at which to begin storing data from the current stream.

count
Int32

The maximum number of bytes to read.

Returns

The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

Exceptions

buffer is null.

offset or count is negative.

offset subtracted from the buffer length is less than count.

The current stream instance is closed.

Examples

This code example is part of a larger example provided for the MemoryStream class.

// Read the first 20 bytes from the stream.
byteArray = gcnew array<Byte>(memStream->Length);
count = memStream->Read( byteArray, 0, 20 );
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
' Read the first 20 bytes from the stream.
byteArray = _
    New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)

Remarks

This method overrides Read.

The offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. The count parameter gives the maximum number of bytes to read from the current stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached.

If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.

The Read method will return zero only if the end of the stream is reached. In all other cases, Read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to Read, the Read method returns zero (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

Caution

If the byte array specified in the buffer parameter is the underlying buffer returned by the GetBuffer method, the array contents are overwritten, and no exception is thrown.

See also

Applies to