public Endpoint ReadEndpoint(InputStream istr)
        {
            lock (this)
            {
                var type = (EndpointType)istr.ReadShort();

                IEndpointFactory?factory = GetEndpointFactory(type);
                Endpoint?        e       = null;

                (Encoding encoding, int size) = istr.StartEndpointEncapsulation();
                if (factory != null)
                {
                    e = factory.Read(istr);
                }

                // If the factory failed to read the endpoint, return an opaque endpoint. This can
                // occur if for example the factory delegates to another factory and this factory
                // isn't available. In this case, the factory needs to make sure the stream position
                // is preserved for reading the opaque endpoint.
                if (e == null)
                {
                    byte[] data = new byte[size];
                    istr.ReadSpan(data);
                    e = new OpaqueEndpointI(type, encoding, data);
                }
                istr.EndEndpointEncapsulation();

                return(e);
            }
        }
        public Endpoint ReadEndpoint(InputStream istr)
        {
            lock (this)
            {
                var type = (EndpointType)istr.ReadShort();

                IEndpointFactory?factory = GetEndpointFactory(type);
                Endpoint?        e       = null;

                (Encoding encoding, int size) = istr.StartEndpointEncapsulation();
                if (factory != null)
                {
                    e = factory.Read(istr);
                }

                // If the factory failed to read the endpoint, return an opaque endpoint. This can
                // occur if for example the factory delegates to another factory and this factory
                // isn't available. In this case, the factory needs to make sure the stream position
                // is preserved for reading the opaque endpoint.
                if (e == null)
                {
                    byte[] data = new byte[size];
                    size = istr.ReadSpan(data);
                    if (size < data.Length)
                    {
                        throw new InvalidDataException(@$ "not enough bytes available reading opaque endpoint, requested {
                            data.Length} bytes, but there was only {size} bytes remaining");
                    }
                    e = new OpaqueEndpoint(type, encoding, data);
                }
                istr.EndEndpointEncapsulation();

                return(e);
            }
 public override int Read(Span <byte> buffer)
 {
     try
     {
         return(_stream.ReadSpan(buffer));
     }
     catch (Exception ex)
     {
         throw new IOException("could not read from stream", ex);
     }
 }