示例#1
0
        public DataStreamContext(Uri u)
        {
            string[] parts = u.PathAndQuery.Split(new [] { ',' }, 2);
            if (parts.Length != 2)
            {
                throw RuntimeFailure.NotValidDataUri();
            }

            var ct = Regex.Replace(parts[0], ";base64", string.Empty);

            if (ct.Length == 0)
            {
                _contentType = new ContentType("text", "plain");
            }
            else
            {
                _contentType = ContentType.Parse(ct);
            }

            byte[] buffer;

            _isBase64 = ct.Length < parts[0].Length; // implied by replacement
            if (_isBase64)
            {
                buffer = Convert.FromBase64String(parts[1]);
            }
            else
            {
                buffer = System.Text.Encoding.ASCII.GetBytes(WebUtility.UrlDecode(parts[1]));
            }

            _baseUri = string.Concat("data:",
                                     _contentType,
                                     _isBase64 ? ";base64" : string.Empty,
                                     ",");
            _data = new MemoryStream(buffer.Length);
            _data.Write(buffer, 0, buffer.Length);
        }