{"id":5547,"date":"2017-12-15T11:25:37","date_gmt":"2017-12-15T11:25:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5547"},"modified":"2017-12-15T11:25:37","modified_gmt":"2017-12-15T11:25:37","slug":"reading-the-bom-preamble","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/reading-the-bom-preamble\/","title":{"rendered":"Reading the BOM\/preamble"},"content":{"rendered":"<p>Sometimes we get a file with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Byte_order_mark\" rel=\"noopener\" target=\"_blank\">BOM<\/a> (or preamble) bytes at the start of the file, which denote a UNICODE encoded file. We don&#8217;t always care these and want to simple remove the BOM (if one exists). <\/p>\n<p>Here&#8217;s some fairly simple code which shows the reading of a stream or file with code to &#8220;skip the BOM&#8221; at the bottom<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (var stream = \r\n   File.Open(currentLogFile, \r\n      FileMode.Open, \r\n      FileAccess.Read, \r\n      FileShare.ReadWrite))\r\n{\r\n   var length = stream.Length;\r\n   var bytes = new byte&#x5B;length];\r\n   var numBytesToRead = (int)length;\r\n   var numBytesRead = 0;\r\n   do\r\n   {\r\n      \/\/ read the file in chunks of 1024\r\n      var n = stream.Read(\r\n         bytes, \r\n         numBytesRead, \r\n         Math.Min(1024, numBytesToRead));\r\n\r\n      numBytesRead += n;\r\n      numBytesToRead -= n;\r\n\r\n   } while (numBytesToRead &gt; 0);\r\n\r\n   \/\/ skip the BOM\r\n   var bom = new UTF8Encoding(true).GetPreamble();\r\n                    \r\n   return bom.Where((b, i) =&gt; b != bytes&#x5B;i]).Any() ? \r\n      bytes : \r\n      bytes.Skip(bom.Length).ToArray();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we get a file with the BOM (or preamble) bytes at the start of the file, which denote a UNICODE encoded file. We don&#8217;t always care these and want to simple remove the BOM (if one exists). Here&#8217;s some fairly simple code which shows the reading of a stream or file with code to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,3],"tags":[],"class_list":["post-5547","post","type-post","status-publish","format-standard","hentry","category-net","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5547"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5547\/revisions"}],"predecessor-version":[{"id":5659,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5547\/revisions\/5659"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}