ReadByteLine() public method

Reads a series of bytes delimited by the byte encoding of newline for this platform. the newline bytes will not be included in the return data.
public ReadByteLine ( ) : byte[]
return byte[]
        public void CanReadByteLineOnMixedAsciiAndUTF8Text()
        {
            var reader = new RebufferableBinaryReader(TestUtil.StringToStreamNoBom("Bonjour poignée"), Encoding.UTF8);
            byte[] bytes = reader.ReadByteLine();
            var expected = new byte[] {66, 111, 110, 106, 111, 117, 114, 32, 112, 111, 105, 103, 110, 195, 169, 101};

            foreach (var pair in expected.Zip(bytes, Tuple.Create))
            {
                Assert.AreEqual(pair.Item1, pair.Item2);
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Parses a section of the stream that is known to be parameter data.
        /// </summary>
        /// <param name="parameters">
        ///     The header parameters of this section. "name" must be a valid key.
        /// </param>
        /// <param name="reader">
        ///     The StreamReader to read the data from
        /// </param>
        /// <returns>
        ///     The <see cref="ParameterPart" /> containing the parsed data (name, value).
        /// </returns>
        /// <exception cref="MultipartParseException">
        ///     thrown if unexpected data is found such as running out of stream before hitting the boundary.
        /// </exception>
        private void ParseParameterPart(Dictionary <string, string> parameters, RebufferableBinaryReader reader)
        {
            // Our job is to get the actual "data" part of the parameter and construct
            // an actual ParameterPart object with it. All we need to do is read data into a string
            // untill we hit the boundary
            var          data      = new StringBuilder();
            MemoryStream byteData  = new MemoryStream();
            bool         firstTime = true;

            byte[] lineBytes = reader.ReadByteLine();
            string line      = Encoding.UTF8.GetString(lineBytes);

            while (line != boundary && line != endBoundary)
            {
                if (line == null)
                {
                    throw new MultipartParseException("Unexpected end of stream. Is there an end boundary?");
                }

                if (firstTime)
                {
                    byteData.Write(lineBytes, 0, lineBytes.Length);
                    data.Append(line);
                    firstTime = false;
                }
                else
                {
                    byteData.Write(lineBytes, 0, lineBytes.Length);
                    data.Append(Environment.NewLine);
                    data.Append(line);
                }
                line = reader.ReadLine();
            }

            if (line == endBoundary)
            {
                readEndBoundary = true;
            }

            // If we're here we've hit the boundary and have the data!
            byteData.Position = 0;
            var part = new ParameterPart(parameters["name"], data.ToString(), byteData);

            ParameterHandler(part);
        }