Ejemplo n.º 1
0
        public string ReadLine()
        {
            while (true)
            {
                String line = lineParser.GetLine();
                if (line != null)
                {
                    return(line);
                }

                Int32 bytesRead = stream.Read(receiveBuffer, 0, receiveBuffer.Length);
                if (bytesRead <= 0)
                {
                    return(null);
                }

                lineParser.Add(receiveBuffer, 0, (UInt32)bytesRead);
            }
        }
Ejemplo n.º 2
0
        public String ReadLine()
        {
            while (true)
            {
                String line = lineParser.GetLine();
                if (line != null)
                {
                    return(line);
                }

                Int32 bytesRead = socket.Receive(receiveBuffer);
                if (bytesRead <= 0)
                {
                    return(null);
                }

                lineParser.Add(receiveBuffer, 0, (UInt32)bytesRead);
            }
        }
Ejemplo n.º 3
0
        // Returns false on error
        public Boolean TryReceive(Socket socket, Buf safeBuffer)
        {
            int bytesReceived;

            try
            {
                bytesReceived = socket.Receive(safeBuffer.array);
            }
            catch (SocketException)
            {
                bytesReceived = -1;
            }

            if (bytesReceived <= 0)
            {
                return(false); // Error
            }

            lineParser.Add(safeBuffer.array, 0, (uint)bytesReceived);
            return(true); // Success
        }
Ejemplo n.º 4
0
        public void TestMethod()
        {
            Encoding   encoding   = Encoding.ASCII;
            LineParser lineParser = new LineParser(encoding, 3);

            lineParser.Add(encoding.GetBytes("abcd\n"));
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());

            lineParser.Add(encoding.GetBytes("abcd\r\n"));
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());

            lineParser.Add(encoding.GetBytes("abcd\nefgh\r\n"));
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.AreEqual("efgh", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());

            lineParser.Add(encoding.GetBytes("abcd\r\nefghijkl"));
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());
            lineParser.Add(encoding.GetBytes("\n"));
            Assert.AreEqual("efghijkl", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());

            lineParser.Add(encoding.GetBytes("abcd\n"));
            lineParser.Add(encoding.GetBytes("abcd\r\n"));
            lineParser.Add(encoding.GetBytes("abcd\n"));
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());

            lineParser.Add(encoding.GetBytes("a"));
            Assert.IsNull(lineParser.GetLine());
            lineParser.Add(encoding.GetBytes("bc"));
            Assert.IsNull(lineParser.GetLine());
            lineParser.Add(encoding.GetBytes("d"));
            Assert.IsNull(lineParser.GetLine());
            lineParser.Add(encoding.GetBytes("\r\ntu"));
            lineParser.Add(encoding.GetBytes("v"));
            Assert.AreEqual("abcd", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());
            lineParser.Add(encoding.GetBytes("\r"));
            Assert.IsNull(lineParser.GetLine());
            lineParser.Add(encoding.GetBytes("\n"));
            Assert.AreEqual("tuv", lineParser.GetLine());
            Assert.IsNull(lineParser.GetLine());
        }
Ejemplo n.º 5
0
        protected static Dictionary <IniSectionKeyPair, String> ParseIniStream(Stream stream, Encoding encoding, UInt32 readBufferSize, out HashSet <String> sections)
        {
            sections = new HashSet <String>();
            Dictionary <IniSectionKeyPair, String> keyPairs = new Dictionary <IniSectionKeyPair, String>();
            String currentSection = null;

            LineParser lineParser = new LineParser(encoding, readBufferSize);

            byte[] readBuffer = new Byte[readBufferSize];

            while (true)
            {
                Int32 bytesRead = stream.Read(readBuffer, 0, readBuffer.Length);
                if (bytesRead <= 0)
                {
                    break;
                }

                lineParser.Add(readBuffer, 0, (UInt32)bytesRead);

                String line;
                while ((line = lineParser.GetLine()) != null)
                {
                    line = line.Trim();
                    if (line.Length > 0)
                    {
                        //String lineUpperCase = line.ToUpper();

                        if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            currentSection = line.Substring(1, line.Length - 2).ToUpper();
                            sections.Add(currentSection);
                        }
                        else
                        {
                            String[] keyPair = line.Split(StringSplitByEquals, 2);
                            if (keyPair == null)
                            {
                                throw new InvalidOperationException(String.Format("String.Split byte '=' returned null of line '{0}'", line));
                            }

                            IniSectionKeyPair sectionPair;
                            sectionPair.section = currentSection;

                            String value;
                            if (keyPair.Length == 1)
                            {
                                sectionPair.key = keyPair[0].ToUpper();
                                value           = null;
                            }
                            else if (keyPair.Length == 2)
                            {
                                sectionPair.key = keyPair[0].ToUpper();
                                value           = keyPair[1];
                            }
                            else
                            {
                                throw new InvalidOperationException(String.Format(
                                                                        "Expected String.Split by '=' to return 1 or 2 for line '{0}' but it returned {1}", line, keyPair.Length));
                            }

                            keyPairs.Add(sectionPair, value);
                        }
                    }
                }
            }

            return(keyPairs);
        }