protected virtual void FlushBuffer() {
try {
// Output all characters (except for previous characters stored at beginning of buffer)
if ( !writeToNull ) {
Debug.Assert( stream != null);
stream.Write( bufBytes, 1, bufPos - 1 );
}
}
catch {
// Future calls to flush (i.e. when Close() is called) don't attempt to write to stream
writeToNull = true;
throw;
}
finally {
// Move last buffer character to the beginning of the buffer (so that previous character can always be determined)
bufBytes[0] = bufBytes[bufPos - 1];
if ( IsSurrogateByte( bufBytes[0] ) ) {
// Last character was the first byte in a surrogate encoding, so move last three
// bytes of encoding to the beginning of the buffer.
bufBytes[1] = bufBytes[bufPos];
bufBytes[2] = bufBytes[bufPos + 1];
bufBytes[3] = bufBytes[bufPos + 2];
}
// Reset buffer position
textPos = (textPos == bufPos) ? 1 : 0;
attrEndPos = (attrEndPos == bufPos) ? 1 : 0;
contentPos = 0; // Needs to be zero, since overwriting '>' character is no longer possible
cdataPos = 0; // Needs to be zero, since overwriting ']]>' characters is no longer possible
bufPos = 1; // Buffer position starts at 1, because we need to be able to safely step back -1 in case we need to
// close an empty element or in CDATA section detection of double ]; _BUFFER[0] will always be 0
}
}