locked
How to get the Encoding instance used by a BinaryReader RRS feed

  • Question

  • A method receives an instance of System.IO.BinaryReader as a parameter (this BinaryReader instance is created elsewhere and I have no control over such creation). The method must call the BinaryReader.Read method to fill a byte[] buffer. A subset of this buffer must be processed as a System.String and a call to the System.Text.Encoding.GetString(byte[], int, int) should be made to the Encoding instance actually used by the BinaryReader instance —such instance might be created by the BinaryReader(Stream, Encoding) constructor.

    How can I get the Encoding instance actually used by the BinaryReader instance?

    Thank you very much in advance for your help.

    Saturday, July 23, 2016 2:49 PM

Answers

  • I don't know why would someone want to retrieve the encoding of BinaryReader. The class has basestream exposed, if you have need you should create your own reader on it instead.

    Btw, if the class passes the BinaryReader to you directly, IMO you should just use it. If you just need the string value, you should call .GetString() instead of meddle it yourself. If you know the specific length to read, just call .GetChars() and convert it to string via "new String(char[])" constructor.


    Monday, August 8, 2016 1:32 AM
    Answerer

All replies

  • Hi MarcoDorantes,

    >> How can I get the Encoding instance actually used by the BinaryReader instance?

    As far as I know, BinaryReader does not provide a method which retrieve the Encoding instance.

    For the following link we know that BinaryReader provides three Constructors ( BinaryReader(Stream),  BinaryReader(Stream, Encoding), BinaryReader(Stream, Encoding, Boolean)).

    For the first Constructor, we know initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.

    For the second and third Constructors, we need to pass related Encoding instance on Constructors.

    https://msdn.microsoft.com/en-us/library/system.io.binaryreader(v=vs.110).aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 25, 2016 6:35 AM
  • Thank you Cole Wu for your response. BinaryReader is clearly missing the capability to retrieve the Encoding instance actually used by it. Should I file this feature request somewhere or MS is working on this feature already?

    Best regards.

    Tuesday, July 26, 2016 4:21 PM
  • Hi MarcoDorantes,

    >>Should I file this feature request somewhere or MS is working on this feature already?

    As I said, please check BinaryReader 's Constructors, if we don't pass Encoding instance, it use UTF-8 encoding by default. If we pass Encoding instance on Constructors. we could know what kind of Encoding instance. so that BinaryReader does not need a property to retrieve the instance of Encoding.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 27, 2016 1:23 AM
  • Hi Cole Wu. You said:

    "If we pass Encoding instance on Constructors. we could know what kind of Encoding instance. "

    That was my initial question: how can I get the actual Encoding instance used by a BinaryReader constructed somewhere else (which I don't control and I don't know how was this BinaryReader constructed).

    Thank you very much in advance for your help.

    Tuesday, August 2, 2016 4:24 PM
  • Check if this limited solution works in your environment:

    BinaryReader binary_reader = . . .
    var df = typeof( BinaryReader ).GetField( "m_decoder", BindingFlags.Instance | BindingFlags.NonPublic );
    var d = df.GetValue( binary_reader );
    var ef = d.GetType().GetField( "m_encoding", BindingFlags.Instance | BindingFlags.NonPublic );
    Encoding encoding = (Encoding)ef.GetValue( d );
    

    Wednesday, August 3, 2016 5:19 AM
  • Thank you very much Viorel for your response.

    That looks like an unsupported solution. If that is the case then I should not use it as part of my business solution.

    BinaryReader is clearly missing the capability to retrieve the Encoding instance actually used by it.

    Is the BinaryReader open source at github? I could submit a Pull Request that fix the problem.

    Friday, August 5, 2016 3:36 PM
  • I don't know why would someone want to retrieve the encoding of BinaryReader. The class has basestream exposed, if you have need you should create your own reader on it instead.

    Btw, if the class passes the BinaryReader to you directly, IMO you should just use it. If you just need the string value, you should call .GetString() instead of meddle it yourself. If you know the specific length to read, just call .GetChars() and convert it to string via "new String(char[])" constructor.


    Monday, August 8, 2016 1:32 AM
    Answerer
  • Thank you cheong00, for your comment.

    Have you seen the Remarks section at the MSDN page for the BinaryReader.BaseStream property?

    https://msdn.microsoft.com/en-us/library/system.io.binaryreader.basestream(v=vs.110).aspx

    It does not sound like an option.

    On the other hand, its ReadChars() method looks promising. I will give it a try. Thank you for your help.

    Monday, August 8, 2016 10:01 AM