Esempio n. 1
0
        public void OpenFile(string JSONFileName, IValue Encoding = null)
        {
            if (IsOpen())
            {
                Close();
            }

            StreamReader _fileReader;

            try
            {
                if (Encoding != null)
                {
                    _fileReader = Environment.FileOpener.OpenReader(JSONFileName, TextEncodingEnum.GetEncoding(Encoding));
                }
                else
                {
                    _fileReader = Environment.FileOpener.OpenReader(JSONFileName);
                }
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.Message, e);
            }

            _reader = new JsonTextReader(_fileReader);
        }
Esempio n. 2
0
        public void WriteLine(string line, IValue encoding = null, string lineSplitter = null)
        {
            // TODO: Для экономии времени не поддерживаем пока конвертируемый разделитель строк
            // Кому надо - попросит PR.

            if (encoding == null)
            {
                _binaryWriter.Write(line);
            }
            else
            {
                var enc   = TextEncodingEnum.GetEncoding(encoding, _writeBOM);
                var bytes = enc.GetBytes(line);
                _binaryWriter.Write(bytes, 0, bytes.Length);
            }

            if (lineSplitter == null)
            {
                _binaryWriter.Write(LineSplitter);
            }
            else
            {
                _binaryWriter.Write(lineSplitter);
            }
        }
Esempio n. 3
0
        public HttpRequestBodyString(string body, IValue encoding = null, ByteOrderMarkUsageEnum bomUsage = ByteOrderMarkUsageEnum.Auto)
        {
            _data = body;

            var addBom = false;

            if (encoding == null)
            {
                if (bomUsage == ByteOrderMarkUsageEnum.Use)
                {
                    addBom = true;
                }

                _encoding = new UTF8Encoding(addBom);
            }
            else
            {
                if (encoding.AsString().Equals("utf-16", StringComparison.OrdinalIgnoreCase) ||
                    encoding.AsString().Equals("utf-32", StringComparison.OrdinalIgnoreCase) && bomUsage == ByteOrderMarkUsageEnum.Auto)
                {
                    addBom = true;
                }
                else
                {
                    addBom = bomUsage == ByteOrderMarkUsageEnum.Use;
                }

                _encoding = TextEncodingEnum.GetEncoding(encoding, addBom);
            }
        }
Esempio n. 4
0
        public IValue GetBodyAsString(IValue encoding = null)
        {
            if (_body == null)
            {
                return(ValueFactory.Create());
            }

            Encoding enc;

            if (encoding == null)
            {
                if (String.IsNullOrEmpty(_defaultCharset))
                {
                    _defaultCharset = "utf-8";
                }

                enc = Encoding.GetEncoding(_defaultCharset);
            }
            else
            {
                enc = TextEncodingEnum.GetEncoding(encoding);
            }

            using (var reader = new StreamReader(_body.OpenReadStream(), enc))
            {
                return(ValueFactory.Create(reader.ReadToEnd()));
            }
        }
Esempio n. 5
0
        public void SetString(string encoding = null)
        {
            Encoding enc = TextEncodingEnum.GetEncodingByName(encoding, true);

            _stringWriter = new StringWriterWithEncoding(enc);
            _writer       = new XmlTextWriter(_stringWriter);
            SetDefaultOptions();
        }
Esempio n. 6
0
        public string GetBodyAsString(IValue encoding = null)
        {
            Encoding enc = (encoding == null || ValueFactory.Create().Equals(encoding))
                    ? new UTF8Encoding(false)
                    : TextEncodingEnum.GetEncoding(encoding);

            return(enc.GetString(_post_raw));
        }
Esempio n. 7
0
 public HttpRequestBodyString(string body, IValue encoding = null)
 {
     _data = body;
     if (encoding == null)
     {
         _encoding = new UTF8Encoding(true);
     }
     else
     {
         _encoding = TextEncodingEnum.GetEncoding(encoding);
     }
 }
Esempio n. 8
0
 public void WriteChars(string line, IValue encoding = null)
 {
     if (encoding == null)
     {
         _binaryWriter.Write(line);
     }
     else
     {
         var enc   = TextEncodingEnum.GetEncoding(encoding, _writeBOM);
         var bytes = enc.GetBytes(line);
         _binaryWriter.Write(bytes, 0, bytes.Length);
     }
 }
        public XmlWriterSettings GetClrSettings(bool addBom = false)
        {
            var result = new XmlWriterSettings
            {
                Indent              = Indent,
                IndentChars         = IndentChars,
                NewLineOnAttributes = IndentAttributes,
                Encoding            = TextEncodingEnum.GetEncodingByName(Encoding, addBom),
                NewLineChars        = System.Environment.NewLine,
                NamespaceHandling   = NamespaceHandling.OmitDuplicates
            };

            return(result);
        }
        public HttpRequestBodyBinary(string body, IValue encoding    = null,
                                     ByteOrderMarkUsageEnum bomUsage = ByteOrderMarkUsageEnum.Auto)
        {
            var utfs = new List <string> {
                "utf-16", "utf-32"
            };
            var addBom = utfs.Contains(encoding?.AsString(), StringComparer.OrdinalIgnoreCase) &&
                         bomUsage == ByteOrderMarkUsageEnum.Auto || bomUsage == ByteOrderMarkUsageEnum.Use;

            var encoder = encoding == null ? new UTF8Encoding(addBom) : TextEncodingEnum.GetEncoding(encoding, addBom);

            var byteArray = encoder.GetBytes(body);

            _memoryStream.Write(byteArray, 0, byteArray.Length);
        }
Esempio n. 11
0
        public string ReadChars(int count = 0, IValue encoding = null)
        {
            char[] chars;
            if (encoding == null)
            {
                chars = _reader.ReadChars(count);
            }
            else
            {
                var bytes = _reader.ReadBytes(count * sizeof(char));
                var enc   = TextEncodingEnum.GetEncoding(encoding);
                chars = enc.GetChars(bytes);
            }

            return(new String(chars));
        }
Esempio n. 12
0
        public void OpenFile(string path, string encoding = null, IValue addBOM = null)
        {
            Encoding enc;

            if (addBOM == null)
            {
                enc = TextEncodingEnum.GetEncodingByName(encoding, true);
            }
            else
            {
                enc = TextEncodingEnum.GetEncodingByName(encoding, addBOM.AsBoolean());
            }

            _writer       = new XmlTextWriter(path, enc);
            _stringWriter = null;
            SetDefaultOptions();
        }
Esempio n. 13
0
        public void OpenFile(string fileName, string encoding = null, IValue addBOM = null, IValue settings = null)
        {
            if (IsOpen())
            {
                Close();
            }

            bool bAddBOM = false;

            if (addBOM != null)
            {
                bAddBOM = addBOM.AsBoolean();
            }

            StreamWriter streamWriter;

            try
            {
                if (encoding != null)
                {
                    streamWriter = Environment.FileOpener.OpenWriter(fileName, TextEncodingEnum.GetEncodingByName(encoding, bAddBOM));
                }
                else
                {
                    streamWriter = Environment.FileOpener.OpenWriter(fileName, TextEncodingEnum.GetEncodingByName("UTF-8", bAddBOM));
                }
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.Message, e);
            }


            _writer = new JsonTextWriter(streamWriter);
            if (settings == null)
            {
                SetDefaultOptions();
            }
            else
            {
                SetOptions(settings);
            }

            SetNewLineChars(streamWriter);
        }
        public void SetBodyFromString(string str, IValue encoding = null, IValue useBOM = null)
        {
            // Получаем кодировку
            // useBOM должен иметь тип ИспользованиеByteOrderMark он не реализован. Его не используем
            // Из синтаксис-помощника в режиме совместимости Использовать
            // Из синтаксис помощника если кодировка не задана используем UTF8

            System.Text.Encoding enc = System.Text.Encoding.UTF8;
            if (encoding != null)
            {
                enc = TextEncodingEnum.GetEncoding(encoding);
            }

            _contentCharset = enc.WebName;

            _bodyStream = new System.IO.MemoryStream();
            byte[] buffer = enc.GetBytes(str);
            _bodyStream.Write(buffer, 0, buffer.Length);
            _bodyStream.Seek(0, System.IO.SeekOrigin.Begin);
        }
Esempio n. 15
0
        public string ReadChars(int count = 0, IValue encoding = null)
        {
            if (count == 0)
            {
                count = (int)(_reader.BaseStream.Length - _reader.BaseStream.Position) * sizeof(char);
            }

            char[] chars;
            if (encoding == null)
            {
                chars = _reader.ReadChars(count);
            }
            else
            {
                var enc = TextEncodingEnum.GetEncoding(encoding);
                _reader = new BinaryReader(_reader.BaseStream, enc);
                chars   = _reader.ReadChars(count);
            }

            return(new String(chars));
        }
Esempio n. 16
0
        public string GetBodyAsString(IValue encoding = null)
        {
            // Формируем кодировку как в 1С. Если не указана, смотрим Content-Type. Если там не указана - используем UTF8
            System.Text.Encoding enc = System.Text.Encoding.UTF8;

            if (encoding != null)
            {
                enc = TextEncodingEnum.GetEncoding(encoding);
            }
            else
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("charset=([^\\\"']+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                string charsetString = regex.Match(_httpContext.Request.ContentType).Value;

                if (charsetString != "")
                {
                    // Что-то нашли
                    try
                    {
                        //charsetString.Substring(8) делает "charset=Кодировка" -> "Кодировка"
                        enc = TextEncodingEnum.GetEncodingByName(charsetString.Substring(8));
                    }
                    catch
                    {
                        // что то не так, осталась UTF8
                    }
                }
            }

            System.IO.Stream str = _httpContext.Request.InputStream;
            int bytes_count      = Convert.ToInt32(str.Length);

            byte[] buffer = new byte[bytes_count];

            str.Seek(0, System.IO.SeekOrigin.Begin);
            str.Read(buffer, 0, bytes_count);

            return(enc.GetString(buffer));
        }