示例#1
0
        public static Encoding GetEncodingEncoding(string name)
        {
            String nameU = name.ToUpperInvariant();

            if (nameU.Equals("UNICODEBIGUNMARKED"))
            {
                return(new UnicodeEncoding(true, false));
            }
            if (nameU.Equals("UNICODEBIG"))
            {
                return(new UnicodeEncoding(true, true));
            }
            if (nameU.Equals("UNICODELITTLEUNMARKED"))
            {
                return(new UnicodeEncoding(false, false));
            }
            if (nameU.Equals("UNICODELITTLE"))
            {
                return(new UnicodeEncoding(false, true));
            }
            Encoding enc;

            if (map.ContainsKey(nameU))
            {
                enc = EncodingUtil.GetEncoding(map[nameU], new EncoderReplacementFallback(""), new DecoderReplacementFallback());
            }
            else
            {
                enc = EncodingUtil.GetEncoding(name, new EncoderReplacementFallback(""), new DecoderReplacementFallback());
            }
            return(enc);
        }
示例#2
0
 public static bool CharsetIsSupported(string charset)
 {
     try
     {
         var enc = EncodingUtil.GetEncoding(charset);
         return(true);
     }
     catch (ArgumentException)
     {
         return(false);
     }
 }
示例#3
0
 /// <exception cref="System.ArgumentException"/>
 public static StreamWriter CreatePrintWriter(Stream output, String encoding)
 {
     return(new StreamWriter(output, EncodingUtil.GetEncoding(encoding)));
 }
示例#4
0
        public virtual void Load(Stream inStream)
        {
            if (inStream == null)
            {
                return;
            }
            StreamReader inp = new StreamReader(inStream, EncodingUtil.GetEncoding(1252));

            while (true)
            {
                // Get next line
                String line = inp.ReadLine();
                if (line == null)
                {
                    return;
                }

                if (line.Length > 0)
                {
                    // Find start of key
                    int len = line.Length;
                    int keyStart;
                    for (keyStart = 0; keyStart < len; keyStart++)
                    {
                        if (whiteSpaceChars.IndexOf(line[keyStart]) == -1)
                        {
                            break;
                        }
                    }

                    // Blank lines are ignored
                    if (keyStart == len)
                    {
                        continue;
                    }

                    // Continue lines that end in slashes if they are not comments
                    char firstChar = line[keyStart];
                    if ((firstChar != '#') && (firstChar != '!'))
                    {
                        while (ContinueLine(line))
                        {
                            String nextLine = inp.ReadLine();
                            if (nextLine == null)
                            {
                                nextLine = "";
                            }
                            String loppedLine = line.Substring(0, len - 1);
                            // Advance beyond whitespace on new line
                            int startIndex;
                            for (startIndex = 0; startIndex < nextLine.Length; startIndex++)
                            {
                                if (whiteSpaceChars.IndexOf(nextLine[startIndex]) == -1)
                                {
                                    break;
                                }
                            }
                            nextLine = nextLine.Substring(startIndex, nextLine.Length - startIndex);
                            line     = loppedLine + nextLine;
                            len      = line.Length;
                        }

                        // Find separation between key and value
                        int separatorIndex;
                        for (separatorIndex = keyStart; separatorIndex < len; separatorIndex++)
                        {
                            char currentChar = line[separatorIndex];
                            if (currentChar == '\\')
                            {
                                separatorIndex++;
                            }
                            else if (keyValueSeparators.IndexOf(currentChar) != -1)
                            {
                                break;
                            }
                        }

                        // Skip over whitespace after key if any
                        int valueIndex;
                        for (valueIndex = separatorIndex; valueIndex < len; valueIndex++)
                        {
                            if (whiteSpaceChars.IndexOf(line[valueIndex]) == -1)
                            {
                                break;
                            }
                        }

                        // Skip over one non whitespace key value separators if any
                        if (valueIndex < len)
                        {
                            if (strictKeyValueSeparators.IndexOf(line[valueIndex]) != -1)
                            {
                                valueIndex++;
                            }
                        }

                        // Skip over white space after other separators if any
                        while (valueIndex < len)
                        {
                            if (whiteSpaceChars.IndexOf(line[valueIndex]) == -1)
                            {
                                break;
                            }
                            valueIndex++;
                        }
                        String key   = line.Substring(keyStart, separatorIndex - keyStart);
                        String value = (separatorIndex < len) ? line.Substring(valueIndex, len - valueIndex) : "";

                        // Convert then store key and value
                        key   = LoadConvert(key);
                        value = LoadConvert(value);
                        Add(key, value);
                    }
                }
            }
        }
示例#5
0
 public static String GetStringForBytes(byte[] bytes, int offset, int length, String encoding)
 {
     return(EncodingUtil.GetEncoding(encoding).GetString(bytes, offset, length));
 }