Sfm2Xml.ByteReader.CheckforAndHandleBOM C# (CSharp) Method

CheckforAndHandleBOM() private method

This will look at the start of the file for the BOM and process it if it's found.
private CheckforAndHandleBOM ( ) : void
return void
		private void CheckforAndHandleBOM()
		{
			byte[] utf32be = new byte[] { 0x00, 0x00, 0xfe, 0xff };
			byte[] utf32le = new byte[] { 0xff, 0xfe, 0x00, 0x00 };
			byte[] utf16be = new byte[] { 0xfe, 0xff };
			byte[] utf16le = new byte[] { 0xff, 0xfe };
			byte[] utf8    = new byte[] { 0xef, 0xbb, 0xbf };

			if (IsCurrentData(utf32le) || IsCurrentData(utf32be))
			{
				m_foundBOM = true;
				m_BOMLength = utf32be.Length;
				//m_BOMEncoding = System.Text.Encoding.???;	// not currently defined for 32
				m_position = m_BOMLength;
			}
			else if (IsCurrentData(utf8))
			{
				m_foundBOM = true;
				m_BOMLength = utf8.Length;
				m_BOMEncoding = System.Text.Encoding.UTF8;
				m_position = m_BOMLength;
			}
			else if (IsCurrentData(utf16le))
			{
				m_foundBOM = true;
				m_BOMLength = utf16le.Length;
				m_BOMEncoding = System.Text.Encoding.Unicode;
				m_position = m_BOMLength;
			}
			else if (IsCurrentData(utf16be))
			{
				m_foundBOM = true;
				m_BOMLength = utf16be.Length;
				m_BOMEncoding = System.Text.Encoding.BigEndianUnicode;
				m_position = m_BOMLength;
			}

			if (m_foundBOM)	// has one
			{
				if (m_BOMEncoding == System.Text.Encoding.UTF8)
				{
					// no extra processing needed for UTF8 - this is the default format
				}
				else if (m_BOMEncoding == System.Text.Encoding.Unicode ||
					m_BOMEncoding == System.Text.Encoding.BigEndianUnicode)
				{
					System.Text.UTF8Encoding utf8Encoder = new System.Text.UTF8Encoding(false, true);
					try
					{
						// decode the wide Unicode byte data to wide chars
						System.Text.Decoder uniDecoder = m_BOMEncoding.GetDecoder();
						int charCount = uniDecoder.GetCharCount(m_FileData, 2, m_FileData.Length-2);
						char[] chars = new Char[charCount];
						uniDecoder.GetChars(m_FileData, 2, m_FileData.Length-2, chars, 0);

						// decode the wide chars to utf8 bytes
						int newLength = utf8Encoder.GetByteCount(chars);
						m_FileData = new byte[newLength];
						utf8Encoder.GetBytes(chars, 0, chars.Length, m_FileData, 0);

						// log msg for user to see
						if (m_Log != null)
							m_Log.AddWarning(String.Format(Sfm2XmlStrings.FileConvertedFrom0To1,
								m_BOMEncoding.WebName, utf8Encoder.WebName));
					}
					catch (System.Exception e)
					{
						if (m_Log != null)
						{
							m_Log.AddFatalError(String.Format(Sfm2XmlStrings.CannotConvertFileFrom0To1,
								m_BOMEncoding.WebName, utf8Encoder.WebName));
							m_Log.AddFatalError(String.Format(Sfm2XmlStrings.Exception0, e.Message));
						}
						m_position = 0;
						m_FileData = new byte[0];	// don't process anything
					}
				}
				else
				{
					m_position = 0;
					m_FileData = new byte[0];	// don't process anything
					if (m_Log != null)
						m_Log.AddFatalError(Sfm2XmlStrings.CannotProcessUtf32Files);
				}
			}
		}