private void Initialize (Stream stream)
{
buffer = new byte [64];
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 '<':
// try to get encoding name from XMLDecl.
if (bufLength >= 5 && GetStringFromBytes (buffer, 1, 4) == "?xml") {
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') {
int remaining = bufLength - bufPos;
if (remaining >= 7 && GetStringFromBytes (buffer, 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;
}
}