public static bool TryParse(byte[] buffer, int available, out ByteOrderMark bom) { if (buffer.Length >= 2) { for (int i = 0; i < table.Length; i++) { bool matched = true; if (available < table[i].Bytes.Length) { continue; } for (int j = 0; j < table[i].Bytes.Length; j++) { if (buffer[j] != table[i].Bytes[j]) { matched = false; break; } } if (matched) { bom = table[i]; return(true); } } } bom = null; return(false); }
public static bool TryParse(byte[] buffer, int available, out ByteOrderMark bom) { if (buffer.Length >= 2) { for (int i = 0; i < table.Length; i++) { bool matched = true; if (available < table[i].Bytes.Length) continue; for (int j = 0; j < table[i].Bytes.Length; j++) { if (buffer[j] != table[i].Bytes[j]) { matched = false; break; } } if (matched) { bom = table[i]; return true; } } } bom = null; return false; }
public void Load(string file) { using (FileStream fs = File.OpenRead(file)) { var buf = new byte[1024]; int nread, i; if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) return; if (ByteOrderMark.TryParse(buf, nread, out bom)) i = bom.Length; else i = 0; do { // Read to the first newline to figure out which line endings this file is using while (i < nread) { if (buf[i] == '\r') { newLine = "\r\n"; break; } else if (buf[i] == '\n') { newLine = "\n"; break; } i++; } if (newLine == null) { if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { newLine = "\n"; break; } i = 0; } } while (newLine == null); // Check for a blank line at the end endsWithEmptyLine = fs.Seek(-1, SeekOrigin.End) > 0 && fs.ReadByte() == '\n'; } // Load the XML document doc = new XmlDocument(); doc.PreserveWhitespace = false; // HACK: XmlStreamReader will fail if the file is encoded in UTF-8 but has <?xml version="1.0" encoding="utf-16"?> // To work around this, we load the XML content into a string and use XmlDocument.LoadXml() instead. string xml = File.ReadAllText(file); doc.LoadXml(xml); }
public static bool TryParse(Stream stream, out ByteOrderMark bom) { var buffer = new byte[4]; int nread; if ((nread = stream.Read(buffer, 0, buffer.Length)) < 2) { bom = null; return(false); } return(TryParse(buffer, nread, out bom)); }
public ProjectWriter(ByteOrderMark bom) { encoding = bom != null?Encoding.GetEncoding(bom.Name) : null; ByteOrderMark = bom; }
public static bool TryParse(Stream stream, out ByteOrderMark bom) { var buffer = new byte[4]; int nread; if ((nread = stream.Read(buffer, 0, buffer.Length)) < 2) { bom = null; return false; } return TryParse(buffer, nread, out bom); }
public ProjectWriter(ByteOrderMark bom) { encoding = bom != null ? Encoding.GetEncoding(bom.Name) : null; ByteOrderMark = bom; }