Example #1
0
        // Detects a Unicode encoding
        private int DetectUnicodeEncoding()
        {
            int mode = this.mode;
            int c1   = this.stream.ReadByte();
            int c2;

            if (c1 < 0)
            {
                return(-1);
            }
            Utf8Reader utf8reader;

            switch (mode)
            {
            case 0:
                // UTF-8 only
                utf8reader  = new Utf8Reader(this.stream, this.errorThrow);
                this.reader = utf8reader;
                utf8reader.Unget(c1);
                c1 = utf8reader.ReadChar();
                if (c1 == 0xfeff && !this.dontSkipUtf8Bom)
                {
                    // Skip BOM
                    c1 = utf8reader.ReadChar();
                }
                return(c1);

            case 1:
            case 3:
                c2 = this.DetectUtf8OrUtf16(c1);
                if (c2 >= -1)
                {
                    return(c2);
                }
                break;

            case 2:
            case 4:
                // UTF-8, UTF-16, or UTF-32
                c2 = this.DetectUtf8Or16Or32(c1);
                if (c2 >= -1)
                {
                    return(c2);
                }
                break;
            }
            // Default case: assume UTF-8
            utf8reader  = new Utf8Reader(this.stream, this.errorThrow);
            this.reader = utf8reader;
            utf8reader.Unget(c1);
            c1 = utf8reader.ReadChar();
            if (!this.dontSkipUtf8Bom && c1 == 0xfeff)
            {
                // Skip BOM
                c1 = utf8reader.ReadChar();
            }
            return(c1);
        }
        // Detects a Unicode encoding
        private int DetectUnicodeEncoding()
        {
            int mode = this.mode;
            int c1   = this.stream.ReadByte();
            int c2;

            if (c1 < 0)
            {
                return(-1);
            }
            Utf8Reader utf8reader;

            if (mode == 0)
            {
                // UTF-8 only
                utf8reader  = new Utf8Reader(this.stream, this.errorThrow);
                this.reader = utf8reader;
                c1          = utf8reader.ReadChar();
                if (c1 == 0xfeff)
                {
                    // Skip BOM
                    c1 = utf8reader.ReadChar();
                }
                return(c1);
            }
            else if (mode == 1 || mode == 3)
            {
                c2 = this.DetectUtf8OrUtf16(c1);
                if (c2 >= -1)
                {
                    return(c2);
                }
            }
            else if (mode == 2 || mode == 4)
            {
                // UTF-8, UTF-16, or UTF-32
                c2 = this.DetectUtf8Or16Or32(c1);
                if (c2 >= -1)
                {
                    return(c2);
                }
            }
            // Default case: assume UTF-8
            utf8reader  = new Utf8Reader(this.stream, this.errorThrow);
            this.reader = utf8reader;
            utf8reader.Unget(c1);
            c1 = utf8reader.ReadChar();
            if (!this.dontSkipUtf8Bom && c1 == 0xfeff)
            {
                // Skip BOM
                c1 = utf8reader.ReadChar();
            }
            return(c1);
        }
Example #3
0
 // Detects a Unicode encoding
 private int DetectUnicodeEncoding()
 {
     int mode = this.mode;
       int c1 = this.stream.ReadByte();
       int c2;
       if (c1 < 0) {
     return -1;
       }
       Utf8Reader utf8reader;
       if (mode == 0) {
     // UTF-8 only
     utf8reader = new Utf8Reader(this.stream, this.errorThrow);
     this.reader = utf8reader;
     c1 = utf8reader.ReadChar();
     if (c1 == 0xfeff) {
       // Skip BOM
       c1 = utf8reader.ReadChar();
     }
     return c1;
       } else if (mode == 1 || mode == 3) {
     c2 = this.DetectUtf8OrUtf16(c1);
     if (c2 >= -1) {
      return c2;
     }
       } else if (mode == 2 || mode == 4) {
     // UTF-8, UTF-16, or UTF-32
     c2 = this.DetectUtf8Or16Or32(c1);
     if (c2 >= -1) {
      return c2;
     }
       }
       // Default case: assume UTF-8
       utf8reader = new Utf8Reader(this.stream, this.errorThrow);
       this.reader = utf8reader;
       utf8reader.Unget(c1);
       c1 = utf8reader.ReadChar();
       if (!this.dontSkipUtf8Bom && c1 == 0xfeff) {
     // Skip BOM
     c1 = utf8reader.ReadChar();
       }
       return c1;
 }