Node.net.Modules.Streams.NodeReadableStream.NodeReadableStream C# (CSharp) Method

NodeReadableStream() private method

Constructs a new NodeReadableStream using the specified .NET stream reader.
private NodeReadableStream ( IronJS env, StreamReader reader ) : System
env IronJS
reader System.IO.StreamReader The .NET StreamReader to wrap.
return System
        internal NodeReadableStream(IronJS.Environment env, StreamReader reader)
            : base(env)
        {
            this.m_Reader = reader;
            EventManager.Add(this);

            // Define the handler to respond to asynchronous operations.
            AsyncCallback handler = null;
            handler = (result) =>
                {
                    if (result.IsCompleted)
                    {
                        int bytes = this.m_Reader.BaseStream.EndRead(result);

                        if (bytes == 0)
                        {
                            // There is no data at the moment.
                        }
                        else if (!this.m_Paused && (this.OnData != null || this.m_PipeDestination != null))
                        {
                            // Send it now.
                            if (this.m_PipeDestination != null)
                                this.m_PipeDestination.write(new NodeBuffer(this.Env, this.m_Buffer, 0, bytes)); // TODO: Support writing as string as well?
                            if (this.OnData != null)
                                this.OnData(this, new DataEventArgs(this.m_Buffer, 0, bytes, this.m_Encoding));
                        }
                        else
                        {
                            // Queue it up.
                            this.m_Queue.Add(new DataEventArgs(this.m_Buffer, 0, bytes, this.m_Encoding));
                        }

                        if (this.m_Reader.BaseStream.Length != this.m_Reader.BaseStream.Position || this.m_Queue.Count > 0)
                            this.m_Reader.BaseStream.BeginRead(this.m_Buffer, 0, 256, handler, null);
                        else
                        {
                            if (this.OnEnd != null)
                                this.OnEnd(this, new EventArgs());
                            EventManager.Remove(this);
                        }
                    }
                };

            // Starts an asynchronous reading operation.
            if (this.m_Reader.BaseStream.Length != this.m_Reader.BaseStream.Position)
                this.m_Reader.BaseStream.BeginRead(this.m_Buffer, 0, 256, handler, null);
            else
            {
                if (this.OnEnd != null)
                    this.OnEnd(this, new EventArgs());
                EventManager.Remove(this);
            }
        }