ReadLine() private method

Reads a single line of input from the stream.
This method should be called in a loop until it returns true.
/// The stream has been disposed. /// /// The operation was canceled via the cancellation token. /// /// An I/O error occurred. ///
private ReadLine ( byte &buffer, int &offset, int &count, CancellationToken cancellationToken ) : bool
buffer byte The buffer containing the line data.
offset int The offset into the buffer containing bytes read.
count int The number of bytes read.
cancellationToken CancellationToken The cancellation token.
return bool
Example #1
0
        /// <summary>
        /// Reads a single line from the <see cref="Pop3Stream"/>.
        /// </summary>
        /// <returns>The line.</returns>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <exception cref="System.InvalidOperationException">
        /// The engine is not connected.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public string ReadLine(CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new InvalidOperationException();
            }

            using (var memory = new MemoryStream()) {
                int    offset, count;
                byte[] buf;

                while (!stream.ReadLine(out buf, out offset, out count, cancellationToken))
                {
                    memory.Write(buf, offset, count);
                }

                memory.Write(buf, offset, count);

                count = (int)memory.Length;
#if !NETFX_CORE
                buf = memory.GetBuffer();
#else
                buf = memory.ToArray();
#endif

                try {
                    return(UTF8.GetString(buf, 0, count));
                } catch (DecoderFallbackException) {
                    return(Latin1.GetString(buf, 0, count));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Reads a single line from the <see cref="Pop3Stream"/>.
        /// </summary>
        /// <returns>The line.</returns>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.InvalidOperationException">
        /// The engine is not connected.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public string ReadLine(CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new InvalidOperationException();
            }

            cancellationToken.ThrowIfCancellationRequested();

            using (var memory = new MemoryStream()) {
                int    offset, count;
                byte[] buf;

                while (!stream.ReadLine(out buf, out offset, out count))
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    memory.Write(buf, offset, count);
                }

                memory.Write(buf, offset, count);

                count = (int)memory.Length;
                buf   = memory.GetBuffer();

                return(Latin1.GetString(buf, 0, count));
            }
        }
Example #3
0
        async Task <string> ReadLineAsync(bool doAsync, CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new InvalidOperationException();
            }

            using (var builder = new ByteArrayBuilder(64)) {
                bool complete;

                do
                {
                    if (doAsync)
                    {
                        complete = await stream.ReadLineAsync(builder, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        complete = stream.ReadLine(builder, cancellationToken);
                    }
                } while (!complete);

                // FIXME: All callers expect CRLF to be trimmed, but many also want all trailing whitespace trimmed.
                builder.TrimNewLine();

                return(builder.ToString());
            }
        }
Example #4
0
        async Task <string> ReadLineAsync(bool doAsync, CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new InvalidOperationException();
            }

            using (var memory = new MemoryStream()) {
                bool   complete;
                byte[] buf;
                int    count;

                do
                {
                    if (doAsync)
                    {
                        complete = await stream.ReadLineAsync(memory, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        complete = stream.ReadLine(memory, cancellationToken);
                    }
                } while (!complete);

                count = (int)memory.Length;
#if !NETFX_CORE && !NETSTANDARD
                buf = memory.GetBuffer();
#else
                buf = memory.ToArray();
#endif

                // Trim the <CR><LF> sequence from the end of the line.
                if (buf[count - 1] == (byte)'\n')
                {
                    count--;

                    if (buf[count - 1] == (byte)'\r')
                    {
                        count--;
                    }
                }

                try {
                    return(UTF8.GetString(buf, 0, count));
                } catch (DecoderFallbackException) {
                    return(Latin1.GetString(buf, 0, count));
                }
            }
        }
Example #5
0
        /// <summary>
        /// Reads a single line from the <see cref="Pop3Stream"/>.
        /// </summary>
        /// <returns>The line.</returns>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <exception cref="System.InvalidOperationException">
        /// The engine is not connected.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public string ReadLine(CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new InvalidOperationException();
            }

            using (var memory = new MemoryStream()) {
                int    offset, count;
                byte[] buf;

                while (!stream.ReadLine(out buf, out offset, out count, cancellationToken))
                {
                    memory.Write(buf, offset, count);
                }

                memory.Write(buf, offset, count);

                count = (int)memory.Length;
#if !NETFX_CORE && !NETSTANDARD
                buf = memory.GetBuffer();
#else
                buf = memory.ToArray();
#endif

                // Trim the <CR><LF> sequence from the end of the line.
                if (buf[count - 1] == (byte)'\n')
                {
                    count--;

                    if (buf[count - 1] == (byte)'\r')
                    {
                        count--;
                    }
                }

                try {
                    return(UTF8.GetString(buf, 0, count));
                } catch (DecoderFallbackException) {
                    return(Latin1.GetString(buf, 0, count));
                }
            }
        }