Question

Say I have an encoding:

Encoding enc;

When this encoding is passed to me, it is set up so it will emit a BOM. I'm not interested in BOMs. Encodings in my system are handled with headers.

Assuming encodings are immutable... I'd like to create a new encoding that exactly matches the existing encoding, but will no longer emit a BOM.

This is so I can avoid the following mismatch:

var data = "áéíóúñ";
var enc = Encoding.UTF8;
long count1 = (long) enc.GetByteCount(data);
long count2;
using(var ms = new MemoryStream())
using(var sw = new StreamWriter(ms, enc))
{
    sw.Write(data);
    sw.Flush();
    count2 = ms.Length;
}
count1.Dump(); //12
count2.Dump(); //15 , oops... BOM was also written
Was it helpful?

Solution

var enc = UTF8Encoding(false); // UTF-8 without BOM

If you don't know the encoding in advance, then you need a bit of extra logic, e.g.

switch(enc.CodePage) {
case 65001:
    enc = UTF8Encoding(false);
    break;
case 1200:
    enc = UnicodeEncoding(false, false);
    break;
case 1201:
    enc = UnicodeEncoding(true, false);
    break;
case 12000:
    enc = UTF32Encoding(false, false);
    break;
case 12001:
    enc = UTF32Encoding(true, false);
    break;
default:
    // pass through the original enc unchanged
}

OTHER TIPS

As per @leppie's comment, R# helped me write a wrapper that suppresses the BOM... reproduced here in case anyone thinks this is a good idea... Given that @ChristianHayter has apparently produced a simpler (and apparently) complete answer, it's probably a good idea to use that instead.

public class EncodingWrapper : Encoding
{
    private readonly Encoding innerEncoding;
    private readonly bool suppressPreamble;
    private static readonly byte[] EmptyBuffer = new byte[0];

    public EncodingWrapper(Encoding innerEncoding, bool suppressPreamble)
    {
        this.innerEncoding = innerEncoding;
        this.suppressPreamble = suppressPreamble;
    }

    public override byte[] GetPreamble()
    {
        return suppressPreamble ? EmptyBuffer : innerEncoding.GetPreamble();
    }

    public override int CodePage
    {
        get { return innerEncoding.CodePage; }
    }
    public override bool IsSingleByte
    {
        get { return innerEncoding.IsSingleByte; }
    }

    public override bool IsMailNewsSave
    {
        get { return innerEncoding.IsMailNewsSave; }
    }

    public override bool IsMailNewsDisplay
    {
        get { return innerEncoding.IsMailNewsDisplay; }
    }

    public override bool IsBrowserSave
    {
        get { return innerEncoding.IsBrowserSave; }
    }

    public override bool IsBrowserDisplay
    {
        get { return innerEncoding.IsBrowserDisplay; }
    }

    public override int WindowsCodePage
    {
        get { return innerEncoding.WindowsCodePage; }
    }

    public override string WebName
    {
        get { return innerEncoding.WebName; }
    }

    public override string HeaderName
    {
        get { return innerEncoding.HeaderName; }
    }

    public override string EncodingName
    {
        get { return innerEncoding.EncodingName; }
    }

    public override string BodyName
    {
        get { return innerEncoding.BodyName; }
    }

    public override string GetString(byte[] bytes, int index, int count)
    {
        return innerEncoding.GetString(bytes, index, count);
    }

    public override string GetString(byte[] bytes)
    {
        return innerEncoding.GetString(bytes);
    }

    public override int GetMaxCharCount(int byteCount)
    {
        return innerEncoding.GetMaxCharCount(byteCount);
    }

    public override int GetMaxByteCount(int charCount)
    {
        return innerEncoding.GetMaxByteCount(charCount);
    }

    public override Encoder GetEncoder()
    {
        return innerEncoding.GetEncoder();
    }

    public override Decoder GetDecoder()
    {
        return innerEncoding.GetDecoder();
    }

    public override bool IsAlwaysNormalized(NormalizationForm form)
    {
        return innerEncoding.IsAlwaysNormalized(form);
    }


    //public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
    //{
    //    return innerEncoding.GetChars(bytes, byteCount, chars, charCount);
    //}

    public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
    {
        return innerEncoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
    }

    public override char[] GetChars(byte[] bytes, int index, int count)
    {
        return innerEncoding.GetChars(bytes, index, count);
    }

    public override char[] GetChars(byte[] bytes)
    {
        return innerEncoding.GetChars(bytes);
    }

    //public override int GetCharCount(byte* bytes, int count)
    //{
    //    return innerEncoding.GetCharCount(bytes, count);
    //}

    public override object Clone()
    {
        return new EncodingWrapper((Encoding)innerEncoding.Clone(), suppressPreamble);
    }

    public override int GetByteCount(string s)
    {
        return innerEncoding.GetByteCount(s);
    }

    public override int GetByteCount(char[] chars)
    {
        return innerEncoding.GetByteCount(chars);
    }

    public override int GetByteCount(char[] chars, int index, int count)
    {
        return innerEncoding.GetByteCount(chars, index, count);
    }

    //public override int GetByteCount(char* chars, int count)
    //{
    //    return innerEncoding.GetByteCount(chars, count);
    //}

    public override byte[] GetBytes(char[] chars)
    {
        return innerEncoding.GetBytes(chars);
    }

    public override byte[] GetBytes(char[] chars, int index, int count)
    {
        return innerEncoding.GetBytes(chars, index, count);
    }

    public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
    {
        return innerEncoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex);
    }

    public override byte[] GetBytes(string s)
    {
        return innerEncoding.GetBytes(s);
    }

    public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
    {
        return innerEncoding.GetBytes(s, charIndex, charCount, bytes, byteIndex);
    }

    //public override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
    //{
    //    return innerEncoding.GetBytes(chars, charCount, bytes, byteCount);
    //}

    public override int GetCharCount(byte[] bytes)
    {
        return innerEncoding.GetCharCount(bytes);
    }

    public override int GetCharCount(byte[] bytes, int index, int count)
    {
        return innerEncoding.GetCharCount(bytes, index, count);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top