Op Englesch liesen Editéieren

Deelen iwwer


PipeStream.Read Method

Definition

Overloads

Read(Span<Byte>)

Reads a sequence of bytes from the current stream, writes them to a byte array, and advances the position within the stream by the number of bytes read.

Read(Byte[], Int32, Int32)

Reads a block of bytes from a stream and writes the data to a specified buffer starting at a specified position for a specified length.

Read(Span<Byte>)

Source:
PipeStream.Unix.cs
Source:
PipeStream.Unix.cs
Source:
PipeStream.Unix.cs

Reads a sequence of bytes from the current stream, writes them to a byte array, and advances the position within the stream by the number of bytes read.

C#
public override int Read(Span<byte> buffer);

Parameters

buffer
Span<Byte>

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

Returns

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

Exceptions

The number of bytes read was longer than the buffer length.

The stream does not support reading.

Cannot access a closed pipe.

The pipe hasn't been connected yet.

-or-

The pipe is in a disconnected state.

-or-

The pipe handle has not been set. (Did your PipeStream implementation call InitializeHandle(SafePipeHandle, Boolean, Boolean)?

Remarks

Use the CanRead property to determine whether the current PipeStream object supports read operations.

Use the ReadAsync method to read asynchronously from the current stream.

This method reads a maximum of buffer.Length bytes from the current stream and stores them in buffer. The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged.

This method will block until at least one byte of data can be read, in the event that no data is available.

This method returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file).

This method 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.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

Read(Byte[], Int32, Int32)

Source:
PipeStream.Unix.cs
Source:
PipeStream.Unix.cs
Source:
PipeStream.Unix.cs

Reads a block of bytes from a stream and writes the data to a specified buffer starting at a specified position for a specified length.

C#
public override int Read(byte[] buffer, int offset, int count);
C#
[System.Security.SecurityCritical]
public override int Read(byte[] buffer, int offset, int count);

Parameters

buffer
Byte[]

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

offset
Int32

The byte offset in the buffer array at which the bytes that are read will be placed.

count
Int32

The maximum number of bytes to read.

Returns

The total number of bytes that are read into buffer. This might be less than the number of bytes requested if that number of bytes is not currently available, or 0 if the end of the stream is reached.

Attributes

Exceptions

buffer is null.

offset is less than 0.

-or-

count is less than 0.

count is greater than the number of bytes available in buffer.

The pipe is closed.

The pipe does not support read operations.

The pipe is disconnected, waiting to connect, or the handle has not been set.

Any I/O error occurred.

Examples

The following example creates an anonymous pipe client and pipe server. The pipe server uses the Read method to read a series of bytes from the pipe client as a validation code. Both the pipe client and the pipe server are part of the same example. The server portion of the example creates a client process and passes it an anonymous pipe handle as an argument.

C#
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeStreamExample
{
    private static byte[] matchSign = {9, 0, 9, 0};

    public static void Main()
    {
        string[] args = Environment.GetCommandLineArgs();
        if (args.Length < 2)
        {
            Process clientProcess = new Process();

            clientProcess.StartInfo.FileName = Environment.CommandLine;

            using (AnonymousPipeServerStream pipeServer =
                new AnonymousPipeServerStream(PipeDirection.In,
                HandleInheritability.Inheritable))
            {
                // Pass the client process a handle to the server.
                clientProcess.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
                clientProcess.StartInfo.UseShellExecute = false;
                Console.WriteLine("[SERVER] Starting client process...");
                clientProcess.Start();

                pipeServer.DisposeLocalCopyOfClientHandle();

                try
                {
                    if (WaitForClientSign(pipeServer))
                    {
                        Console.WriteLine("[SERVER] Valid sign code received!");
                    }
                    else
                    {
                        Console.WriteLine("[SERVER] Invalid sign code received!");
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine("[SERVER] Error: {0}", e.Message);
                }
            }
            clientProcess.WaitForExit();
            clientProcess.Close();
            Console.WriteLine("[SERVER] Client quit. Server terminating.");
        }
        else
        {
            using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, args[1]))
            {
                try
                {
                    Console.WriteLine("[CLIENT] Sending sign code...");
                    SendClientSign(pipeClient);
                }
                catch (IOException e)
                {
                     Console.WriteLine("[CLIENT] Error: {0}", e.Message);
                }
            }
            Console.WriteLine("[CLIENT] Terminating.");
        }
    }

    private static bool WaitForClientSign(PipeStream inStream)
    {
        byte[] inSign = new byte[matchSign.Length];
        int len = inStream.Read(inSign, 0, matchSign.Length);
        bool valid = len == matchSign.Length;

        while (valid && len-- > 0)
        {
            valid = valid && (matchSign[len] == inSign[len]);
        }
        return valid;
    }

    private static void SendClientSign(PipeStream outStream)
    {
        outStream.Write(matchSign, 0, matchSign.Length);
    }
}

Remarks

Use the CanRead property to determine whether the current PipeStream object supports read operations.

Calling the Read method blocks until count bytes are read or the end of the stream is reached. For asynchronous read operations, see BeginRead and EndRead.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1