private void Initialize(Stream stream) { buffer = new byte [6]; this.stream = stream; enc = StrictUTF8; // Default to UTF8 if we can't guess it bufLength = stream.Read(buffer, 0, buffer.Length); if (bufLength == -1 || bufLength == 0) { return; } int c = ReadByteSpecial(); switch (c) { case 0xFF: c = ReadByteSpecial(); if (c == 0xFE) { // BOM-ed little endian utf-16 enc = Encoding.Unicode; } else { // It doesn't start from "<?xml" then its encoding is utf-8 bufPos = 0; } break; case 0xFE: c = ReadByteSpecial(); if (c == 0xFF) { // BOM-ed big endian utf-16 enc = Encoding.BigEndianUnicode; return; } else { // It doesn't start from "<?xml" then its encoding is utf-8 bufPos = 0; } break; case 0xEF: c = ReadByteSpecial(); if (c == 0xBB) { c = ReadByteSpecial(); if (c != 0xBF) { bufPos = 0; } } else { buffer [--bufPos] = 0xEF; } break; case 0: // It could still be 1234/2143/3412 variants of UTF32, but only 1234 version is available on .NET. c = ReadByteSpecial(); if (c == 0) { enc = Strict1234UTF32; } else { enc = StrictBigEndianUTF16; } break; case '<': c = ReadByteSpecial(); if (c == 0) { if (ReadByteSpecial() == 0) { enc = Encoding.UTF32; // little endian UTF32 } else { enc = Encoding.Unicode; // little endian UTF16 } } else if (bufLength >= 4 && GetStringFromBytes(1, 4) == "?xml") { // try to get encoding name from XMLDecl. bufPos += 4; c = SkipWhitespace(); // version. It is optional here. if (c == 'v') { while (c >= 0) { c = ReadByteSpecial(); if (c == '0') // 0 of 1.0 { ReadByteSpecial(); break; } } c = SkipWhitespace(); } if (c == 'e') { if (GetStringFromBytes(bufPos, 7) == "ncoding") { bufPos += 7; c = SkipWhitespace(); if (c != '=') { throw encodingException; } c = SkipWhitespace(); int quoteChar = c; StringBuilder sb = new StringBuilder(); while (true) { c = ReadByteSpecial(); if (c == quoteChar) { break; } else if (c < 0) { throw encodingException; } sb.Append((char)c); } string encodingName = sb.ToString(); if (!XmlChar.IsValidIANAEncoding(encodingName)) { throw encodingException; } enc = Encoding.GetEncoding(encodingName); } } } #if TARGET_JVM else { if (bufLength >= 10 && Encoding.Unicode.GetString(buffer, 2, 8) == "?xml") { enc = Encoding.Unicode; } } #endif bufPos = 0; break; default: bufPos = 0; break; } }
private void Initialize(Stream stream) { this.buffer = new byte[64]; this.stream = stream; this.enc = XmlInputStream.StrictUTF8; this.bufLength = stream.Read(this.buffer, 0, this.buffer.Length); if (this.bufLength == -1 || this.bufLength == 0) { return; } int i = this.ReadByteSpecial(); int num = i; if (num != 254) { if (num != 255) { if (num != 60) { if (num != 239) { this.bufPos = 0; } else { i = this.ReadByteSpecial(); if (i == 187) { i = this.ReadByteSpecial(); if (i != 191) { this.bufPos = 0; } } else { this.buffer[--this.bufPos] = 239; } } } else { if (this.bufLength >= 5 && XmlInputStream.GetStringFromBytes(this.buffer, 1, 4) == "?xml") { this.bufPos += 4; i = this.SkipWhitespace(); if (i == 118) { while (i >= 0) { i = this.ReadByteSpecial(); if (i == 48) { this.ReadByteSpecial(); break; } } i = this.SkipWhitespace(); } if (i == 101) { int num2 = this.bufLength - this.bufPos; if (num2 >= 7 && XmlInputStream.GetStringFromBytes(this.buffer, this.bufPos, 7) == "ncoding") { this.bufPos += 7; i = this.SkipWhitespace(); if (i != 61) { throw XmlInputStream.encodingException; } i = this.SkipWhitespace(); int num3 = i; StringBuilder stringBuilder = new StringBuilder(); for (;;) { i = this.ReadByteSpecial(); if (i == num3) { break; } if (i < 0) { goto Block_19; } stringBuilder.Append((char)i); } string text = stringBuilder.ToString(); if (!XmlChar.IsValidIANAEncoding(text)) { throw XmlInputStream.encodingException; } this.enc = Encoding.GetEncoding(text); goto IL_272; Block_19: throw XmlInputStream.encodingException; } } } IL_272: this.bufPos = 0; } } else { i = this.ReadByteSpecial(); if (i == 254) { this.enc = Encoding.Unicode; } else { this.bufPos = 0; } } } else { i = this.ReadByteSpecial(); if (i == 255) { this.enc = Encoding.BigEndianUnicode; return; } this.bufPos = 0; } }