public HtmlStream(Stream stm, Encoding defaultEncoding)
{
if (defaultEncoding == null) defaultEncoding = Encoding.UTF8; // default is UTF8
if (!stm.CanSeek){
// Need to be able to seek to sniff correctly.
stm = CopyToMemoryStream(stm);
}
this.stm = stm;
rawBuffer = new Byte[BUFSIZE];
rawUsed = stm.Read(rawBuffer, 0, 4); // maximum byte order mark
this.m_buffer = new char[BUFSIZE];
// Check byte order marks
this.m_decoder = AutoDetectEncoding(rawBuffer, ref rawPos, rawUsed);
int bom = rawPos;
if (this.m_decoder == null)
{
this.m_decoder = defaultEncoding.GetDecoder();
rawUsed += stm.Read(rawBuffer, 4, BUFSIZE-4);
DecodeBlock();
// Now sniff to see if there is an XML declaration or HTML <META> tag.
Decoder sd = SniffEncoding();
if (sd != null) {
this.m_decoder = sd;
}
}
// Reset to get ready for Read()
this.stm.Seek(0, SeekOrigin.Begin);
this.pos = this.used = 0;
// skip bom
if (bom>0){
stm.Read(this.rawBuffer, 0, bom);
}
this.rawPos = this.rawUsed = 0;
}