System.Net.WebConnection.ReadLine C# (CSharp) Method

ReadLine() static private method

static private ReadLine ( byte buffer, int &start, int max, string &output ) : bool
buffer byte
start int
max int
output string
return bool
		static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
		{
			bool foundCR = false;
			StringBuilder text = new StringBuilder ();

			int c = 0;
			while (start < max) {
				c = (int) buffer [start++];

				if (c == '\n') {			// newline
					if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
						text.Length--;

					foundCR = false;
					break;
				} else if (foundCR) {
					text.Length--;
					break;
				}

				if (c == '\r')
					foundCR = true;
					

				text.Append ((char) c);
			}

			if (c != '\n' && c != '\r')
				return false;

			if (text.Length == 0) {
				output = null;
				return (c == '\n' || c == '\r');
			}

			if (foundCR)
				text.Length--;

			output = text.ToString ();
			return true;
		}

Usage Example

        private WebHeaderCollection ReadHeaders(HttpWebRequest request, Stream stream, out byte[] retBuffer, out int status)
        {
            retBuffer = null;
            status    = 200;
            byte[]              array        = new byte[1024];
            MemoryStream        memoryStream = new MemoryStream();
            bool                flag         = false;
            int                 num2;
            WebHeaderCollection webHeaderCollection;

            for (;;)
            {
                int num = stream.Read(array, 0, 1024);
                if (num == 0)
                {
                    break;
                }
                memoryStream.Write(array, 0, num);
                num2 = 0;
                string text = null;
                webHeaderCollection = new WebHeaderCollection();
                while (WebConnection.ReadLine(memoryStream.GetBuffer(), ref num2, (int)memoryStream.Length, ref text))
                {
                    if (text == null)
                    {
                        goto Block_2;
                    }
                    if (flag)
                    {
                        webHeaderCollection.Add(text);
                    }
                    else
                    {
                        int num3 = text.IndexOf(' ');
                        if (num3 == -1)
                        {
                            goto Block_5;
                        }
                        status = (int)uint.Parse(text.Substring(num3 + 1, 3));
                        flag   = true;
                    }
                }
            }
            this.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
            return(null);

Block_2:
            if (memoryStream.Length - (long)num2 > 0L)
            {
                retBuffer = new byte[memoryStream.Length - (long)num2];
                Buffer.BlockCopy(memoryStream.GetBuffer(), num2, retBuffer, 0, retBuffer.Length);
            }
            return(webHeaderCollection);

Block_5:
            this.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
            return(null);
        }
All Usage Examples Of System.Net.WebConnection::ReadLine