Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Last active July 25, 2020 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsakamoto/87d5b76387d8287ba6cea0362777c206 to your computer and use it in GitHub Desktop.
Save jsakamoto/87d5b76387d8287ba6cea0362777c206 to your computer and use it in GitHub Desktop.
Wrapper class for System.Text.Encoding object to add missing "BodyName" and "HeaderName" property.

What's this?

This is C# code for .NET Core application.

This code provides BodyNamePatchedEncoding Encoding class for wrapping other encoding object to avoid raising "System.NotSupportedException : No data is available for encoding ????".

This exception is sometimes raised when using the encoding object that is in System.Text.Encoding.CodePages NuGet package, because those encoding classes don't implement "BodyName" and "HeaderName" property.

BodyNamePatchedEncoding class provide a chance to append "BodyName" and "HeaderName" property value.

How to use?

Example)

// Install System.Text.Encoding.CodePages NuGet package into this project,
// and "BodyNamePatchedEncoding.cs" (that is in this gist.)
using System.Text;
...

// At 1st, install appendix code pages into runtime.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
...

// Get encoding object what you need (but "BodyName" property of this object raise exception)...
var iso2022jpBase = Encoding.GetEncoding("iso-2022-jp");
// ...and wrap it with correct "BodyName" and "HeaderName" property value.
var iso2022jp = new BodyNamePatchedEncoding(iso2022jpBase, "iso-2022-jp");

// use "iso2022jp" encoding object.
iso2022jp.BodyName; // -> it returns "iso-2022-jp".

LICENSE

The Unlicense http://unlicense.org/

// LICENSE: The Unlicense
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
namespace System.Text
{
internal class BodyNamePatchedEncoding : Encoding
{
private Encoding InternalEncoding { get; }
private string _BodyName { get; }
public BodyNamePatchedEncoding(Encoding encoding, string bodyName)
: base(encoding.CodePage, encoding.EncoderFallback, encoding.DecoderFallback)
{
this.InternalEncoding = encoding;
this._BodyName = bodyName;
}
public override string BodyName => this._BodyName ?? this.InternalEncoding.BodyName;
public override string HeaderName => this._BodyName ?? this.InternalEncoding.HeaderName;
public override int CodePage => this.InternalEncoding.CodePage;
public override string EncodingName => this.InternalEncoding.EncodingName;
public override bool IsBrowserSave => this.InternalEncoding.IsBrowserSave;
public override bool IsBrowserDisplay => this.InternalEncoding.IsBrowserDisplay;
public override bool IsMailNewsDisplay => this.InternalEncoding.IsMailNewsDisplay;
public override bool IsMailNewsSave => this.InternalEncoding.IsMailNewsSave;
public override bool IsSingleByte => this.InternalEncoding.IsSingleByte;
public override string WebName => this.InternalEncoding.WebName;
public override int WindowsCodePage => this.InternalEncoding.WindowsCodePage;
public override int GetByteCount(char[] chars, int index, int count) => this.InternalEncoding.GetByteCount(chars, index, count);
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) => this.InternalEncoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex);
public override int GetCharCount(byte[] bytes, int index, int count) => this.InternalEncoding.GetCharCount(bytes, index, count);
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) => this.InternalEncoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
public override int GetMaxByteCount(int charCount) => this.InternalEncoding.GetMaxByteCount(charCount);
public override int GetMaxCharCount(int byteCount) => this.InternalEncoding.GetMaxCharCount(byteCount);
public override object Clone() => new BodyNamePatchedEncoding(this.InternalEncoding.Clone() as Encoding, this.BodyName);
public override int GetByteCount(char[] chars) => this.InternalEncoding.GetByteCount(chars);
public override int GetByteCount(string s) => this.InternalEncoding.GetByteCount(s);
public override unsafe int GetByteCount(char* chars, int count) => this.InternalEncoding.GetByteCount(chars, count);
public override byte[] GetBytes(char[] chars) => this.InternalEncoding.GetBytes(chars);
public override byte[] GetBytes(char[] chars, int index, int count) => this.InternalEncoding.GetBytes(chars, index, count);
public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount) => this.InternalEncoding.GetBytes(chars, charCount, bytes, byteCount);
public override byte[] GetBytes(string s) => this.InternalEncoding.GetBytes(s);
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => this.InternalEncoding.GetBytes(s, charIndex, charCount, bytes, byteIndex);
public override int GetCharCount(byte[] bytes) => this.InternalEncoding.GetCharCount(bytes);
public override unsafe int GetCharCount(byte* bytes, int count) => this.InternalEncoding.GetCharCount(bytes, count);
public override char[] GetChars(byte[] bytes) => this.InternalEncoding.GetChars(bytes);
public override char[] GetChars(byte[] bytes, int index, int count) => this.InternalEncoding.GetChars(bytes, index, count);
public override unsafe int GetChars(byte* bytes, int byteCount, char* chars, int charCount) => this.InternalEncoding.GetChars(bytes, byteCount, chars, charCount);
public override string GetString(byte[] bytes) => this.InternalEncoding.GetString(bytes);
public override string GetString(byte[] bytes, int index, int count) => this.InternalEncoding.GetString(bytes, index, count);
public override Decoder GetDecoder() => this.InternalEncoding.GetDecoder();
public override Encoder GetEncoder() => this.InternalEncoding.GetEncoder();
public override int GetHashCode() => this.InternalEncoding.GetHashCode();
public override byte[] GetPreamble() => this.InternalEncoding.GetPreamble();
public override bool IsAlwaysNormalized(NormalizationForm form) => this.InternalEncoding.IsAlwaysNormalized(form);
public override string ToString() => this.InternalEncoding.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment