Example #1
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 #2
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));
                }
            }
        }