private void WriteEncoded(char[] ca, int offset, long length)
 {
     if (_bout == null)
     {
         _bout   = new ByteArrayOutputStream2((int)(2 * length));
         _writer = new BinaryWriter(_bout, __ISO_8859_1);
     }
     else
     {
         _bout.Reset();
     }
     _writer.Write(ca, offset, (int)length);
     _writer.Flush();
     EnsureSpareCapacity(_bout.Length);
     Buffer.BlockCopy(_bout.GetBuffer(), 0, _buf, _size, (int)_bout.Length);
     _size += (int)_bout.Length;
 }
 public override void Write(byte[] buffer, int offset, int count)
 {
     if (_bout == null)
     {
         _bout   = new ByteArrayOutputStream2((int)(2 * count));
         _writer = new BinaryWriter(_bout, __ISO_8859_1);
     }
     else
     {
         _bout.Reset();
     }
     _writer.Write(buffer, offset, count);
     _writer.Flush();
     EnsureSpareCapacity(_bout.Length);
     Buffer.BlockCopy(_bout.GetBuffer(), 0, _buf, _size, (int)_bout.Length);
     _size += (int)_bout.Length;
 }
 private void WriteEncoded(char[] ca,int offset, long length)
 {
     if (_bout == null)
     {
         _bout = new ByteArrayOutputStream2((int)(2 * length));
         _writer = new BinaryWriter(_bout, __ISO_8859_1);
     }
     else
     {
         _bout.Reset();
     }
     _writer.Write(ca,offset,(int)length);
     _writer.Flush();
     EnsureSpareCapacity(_bout.Length);
     Buffer.BlockCopy(_bout.GetBuffer(), 0, _buf, _size, (int)_bout.Length);
     _size+=(int)_bout.Length;
 }
 public override void Write(byte[] buffer, int offset, int count)
 {
     if (_bout == null)
     {
         _bout = new ByteArrayOutputStream2((int)(2 * count));
         _writer = new BinaryWriter(_bout, __ISO_8859_1);
     }
     else
     {
         _bout.Reset();
     }
     _writer.Write(buffer, offset, count);
     _writer.Flush();
     EnsureSpareCapacity(_bout.Length);
     Buffer.BlockCopy(_bout.GetBuffer(), 0, _buf, _size, (int)_bout.Length);
     _size += (int)_bout.Length;
 }
Example #5
0
        /// <summary>
        /// Decoded parameters to Map.
        /// </summary>
        /// <param name="input">the stream containing the encoded parameters</param>
        /// <param name="map"></param>
        /// <param name="charset"></param>
        /// <param name="maxLength"></param>
        public static void DecodeTo(Stream input, MultiMap <string> map, string charset, int maxLength)
        {
            if (charset == null || StringUtil.__ISO_8859_1.Equals(charset))
            {
                Decode88591To(input, map, maxLength);
                return;
            }

            if (StringUtil.__UTF8.Equals(charset, StringComparison.OrdinalIgnoreCase))
            {
                DecodeUtf8To(input, map, maxLength);
                return;
            }

            if (StringUtil.__UTF16.Equals(charset, StringComparison.OrdinalIgnoreCase)) // Should be all 2 byte encodings
            {
                DecodeUtf16To(input, map, maxLength);
                return;
            }


            lock (map)
            {
                string key   = null;
                string value = null;

                int c;
                int digit  = 0;
                int digits = 0;

                int totalLength = 0;
                ByteArrayOutputStream2 output = new ByteArrayOutputStream2();
                input.Position = 0;
                long size = 0;

                while ((c = input.ReadByte()) > 0)
                {
                    switch ((char)c)
                    {
                    case '&':
                        size            = output.Length;
                        value           = size == 0 ? "" : Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size);
                        output.Position = 0;
                        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)
                        {
                            output.WriteByte(c);
                            break;
                        }
                        size            = output.Length;
                        key             = size == 0 ? "" : Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size);
                        output.Position = 0;
                        break;

                    case '+':
                        output.WriteByte(' ');
                        break;

                    case '%':
                        digits = 2;
                        break;

                    default:
                        if (digits == 2)
                        {
                            digit  = TypeUtil.ConvertHexDigit((byte)c);
                            digits = 1;
                        }
                        else if (digits == 1)
                        {
                            int v = (byte)(digit << 4) + TypeUtil.ConvertHexDigit((byte)c);
                            output.WriteByte(v);
                            digits = 0;
                        }
                        else
                        {
                            output.WriteByte((byte)c);
                        }
                        break;
                    }

                    totalLength++;
                    if (maxLength >= 0 && totalLength > maxLength)
                    {
                        throw new InvalidOperationException("Form too large");
                    }
                }

                size = output.Length;
                if (key != null)
                {
                    value           = size == 0 ? "" : Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size);
                    output.Position = 0;
                    map.Append(key, value);
                }
                else if (size > 0)
                {
                    map.Append(Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size), "");
                }
            }
        }