public void TestUtfStringBuilder2()
        {
            string source = "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";
            Utf8StringBuilder buffer = new Utf8StringBuilder();
            buffer.Append(source);

            for (int i = 0; i < 100; i++)
            {
                source += "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";
                buffer.Append("abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty");
            }

            Assert.AreEqual(source, buffer.ToString());
            Assert.IsTrue(buffer.ToString().EndsWith("njetty"));
        }
        public void TestUtfStringBuilder1()
        {
            string source = "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";

            for (int i = 0; i < 100; i++)
            {
                source += "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";
            }

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(source);
            Utf8StringBuilder buffer = new Utf8StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                buffer.Append(bytes[i]);
            }

            Assert.AreEqual(source, buffer.ToString());
            Assert.IsTrue(buffer.ToString().EndsWith("njetty"));
        }
예제 #3
0
        /// <summary>
        /// Decoded parameters to Map.
        /// </summary>
        /// <param name="input">InputSteam to read</param>
        /// <param name="map">MultiMap to Add parameters to</param>
        /// <param name="maxLength">maximum length of conent to read 0r -1 for no limit</param>
        public static void DecodeUtf8To(Stream input, MultiMap <string> map, int maxLength)
        {
            lock (map)
            {
                Utf8StringBuilder buffer = new Utf8StringBuilder();
                string            key    = null;
                string            value  = null;

                int b;

                // TODO cache of parameter names ???
                int totalLength = 0;
                while ((b = input.ReadByte()) >= 0)
                {
                    switch ((char)b)
                    {
                    case '&':
                        value = buffer.Length == 0 ? "" : buffer.ToString();
                        buffer.Reset();
                        if (key != null)
                        {
                            map.Append(key, value);
                        }
                        else if (value != null && value.Length > 0)
                        {
                            map.Append(value, "");
                        }
                        key   = null;
                        value = null;
                        break;

                    case '=':
                        if (key != null)
                        {
                            buffer.Append((byte)b);
                            break;
                        }
                        key = buffer.ToString();
                        buffer.Reset();
                        break;

                    case '+':
                        buffer.Append((byte)' ');
                        break;

                    case '%':
                        int dh = input.ReadByte();
                        int dl = input.ReadByte();
                        if (dh < 0 || dl < 0)
                        {
                            break;
                        }
                        buffer.Append((byte)((TypeUtil.ConvertHexDigit((byte)dh) << 4) + TypeUtil.ConvertHexDigit((byte)dl)));
                        break;

                    default:
                        buffer.Append((byte)b);
                        break;
                    }
                    if (maxLength >= 0 && (++totalLength > maxLength))
                    {
                        throw new InvalidOperationException("Form too large");
                    }
                }

                if (key != null)
                {
                    value = buffer.Length == 0 ? "" : buffer.ToString();
                    buffer.Reset();
                    map.Append(key, value);
                }
                else if (buffer.Length > 0)
                {
                    map.Append(buffer.ToString(), "");
                }
            }
        }
예제 #4
0
        /* -------------------------------------------------------------- */

        /** Decoded parameters to Map.
         * @param data the byte[] containing the encoded parameters
         */
        public static void DecodeUtf8To(byte[] raw, int offset, int length, MultiMap <string> map, Utf8StringBuilder buffer)
        {
            lock (map)
            {
                string key   = null;
                string value = null;

                // TODO cache of parameter names ???
                int end = offset + length;
                for (int i = offset; i < end; i++)
                {
                    byte b = raw[i];
                    switch ((char)(0xff & b))
                    {
                    case '&':
                        value = buffer.Length == 0 ? "" : buffer.ToString();
                        buffer.Reset();
                        if (key != null)
                        {
                            map.Append(key, value);
                        }
                        else if (value != null && value.Length > 0)
                        {
                            map.Append(value, "");
                        }
                        key   = null;
                        value = null;
                        break;

                    case '=':
                        if (key != null)
                        {
                            buffer.Append(b);
                            break;
                        }
                        key = buffer.ToString();
                        buffer.Reset();
                        break;

                    case '+':
                        buffer.Append((byte)' ');
                        break;

                    case '%':
                        if (i + 2 < end)
                        {
                            buffer.Append((byte)((TypeUtil.ConvertHexDigit(raw[++i]) << 4) + TypeUtil.ConvertHexDigit(raw[++i])));
                        }
                        break;

                    default:
                        buffer.Append(b);
                        break;
                    }
                }

                if (key != null)
                {
                    value = buffer.Length == 0 ? "" : buffer.ToString();
                    buffer.Reset();
                    map.Append(key, value);
                }
                else if (buffer.Length > 0)
                {
                    map.Append(buffer.ToString(), "");
                }
            }
        }